Ajax Loader
HTML
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
1
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
2
<div class="paper" style="width: 249px;"></div>
3
<fieldset>
4
  <legend>Colors</legend>
5
  <div class="colorpit"></div>
6
</fieldset>
7
<fieldset>
8
  <legend>Functions</legend>
9
  <center>
10
      <button class="clear">Clear</button>
11
      <br />
12
      <br />
13
      <button class="download">Download as PNG</button>
14
    </center>
15
</fieldset>
 
CSS
body {
1
body {
2
  padding: 10px;
3
  margin: 10px;
4
}
5
fieldset {
6
  width: 120px;
7
  margin-left: 270px;
8
}
9
button {
10
  cursor: default;
11
}
12
.color {
13
  width: 25px;
14
  height: 25px;
15
  float: left;
16
  margin: 1px;
17
  border: 1px solid #DDD;
18
}
19
.paper {
20
  padding: 0px;
21
  float: left;
22
  border-top: 1px solid #DDD;
23
  border-left: 1px solid #DDD;
24
}
25
.paper .tile {
26
  width: 30px;
27
  height: 30px;
28
  border-bottom: 1px solid #DDD;
29
  border-right: 1px solid #DDD;
30
  float: left;
31
}
 
JavaScript
// author: HashNuke
1
// author: HashNuke
2
// github: https://github.com/HashNuke/pixelart-editor
3
 
4
$(document).ready(function() { 
5
  
6
  var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
7
  
8
var PaintInterface;
9
    PaintInterface = (function() {
10
 
11
      PaintInterface.prototype.options = {
12
        tiles: 10,
13
        tileSize: 10,
14
        width: 200,
15
        height: 200
16
      };
17
      
18
      PaintInterface.prototype.drawnTiles = [];
19
 
20
      PaintInterface.prototype.colors = ["ff0000", "ffa500", "ffff00", "00ff00", "00ffff", "0000ff", "a200ff", "ff00ff", "ffffff", "aaaaaa", "555555", "000000"];
21
 
22
      function PaintInterface(colorPitSelector, paperSelector, clearButtonSelector, downloadButtonSelector, options) {
23
        if (options == null) options = {};
24
        this.clearPaper = __bind(this.clearPaper, this);
25
        this.previewPaper = __bind(this.previewPaper, this);
26
        this.downloadImage = __bind(this.downloadImage, this);
27
        this.drawOnClick = __bind(this.drawOnClick, this);
28
        this.prepareCanvas = __bind(this.prepareCanvas, this);
29
        this.colorPit = $(colorPitSelector);
30
        this.paper = $(paperSelector);
31
        this.clearButton = $(clearButtonSelector);
32
        this.downloadButton = $(downloadButtonSelector);
33
        this.setOptions(options);
34
        this.brushColor = this.colors[0];
35
        this.preparePaper();
36
        this.prepareCanvas();
37
        this.prepareColorPit();
38
        this.bindEvents();
39
      }
40
 
41
      PaintInterface.prototype.setOptions = function(options) {
42
        var option, value;
43
        for (option in options) {
44
          value = options[option];
45
          this.options[option] = value;
46
        }
47
        return this.options.width = this.options.height = this.options.tileSize * this.options.tiles;
48
      };
49
 
50
      PaintInterface.prototype.prepareCanvas = function() {
51
        this.canvasElement = $("<canvas/>");
52
        return this.canvasElement.attr({
53
          width: this.options.width,
54
          height: this.options.height
55
        });
56
      };
57
 
58
      PaintInterface.prototype.preparePaper = function() {
59
        var i, _ref;
60
        for (i = 0, _ref = this.options.tiles * this.options.tiles; 0 <= _ref ? i < _ref : i > _ref; 0 <= _ref ? i++ : i--) {
61
          $("<div class='tile'/>").appendTo(this.paper);
62
        }
63
        return this.paper.css({
64
          "width": this.options.width + (this.options.tiles + 1)
65
        });
66
      };
67
 
68
      PaintInterface.prototype.prepareColorPit = function() {
69
        var _this = this;
70
        return this.colors.forEach(function(color) {
71
          return $("<div/>").css('background', "#" + color).prop("class", "color").data("color", "" + color).appendTo(_this.colorPit);
72
        });
73
      };
74
 
75
      PaintInterface.prototype.bindEvents = function() {
76
        var _this = this;
77
        this.colorPit.on("click", ".color", function(e) {
78
          return _this.brushColor = $(e.currentTarget).data("color");
79
        });
80
        this.clearButton.click(this.clearPaper);
81
        this.downloadButton.click(this.downloadImage);
82
        return this.paper.on("click", ".tile", this.drawOnClick);
83
      };
84
 
85
      PaintInterface.prototype.drawOnClick = function(e) {
86
        var $target, x, y;
87
        $target = $(e.currentTarget);
88
        $target.css('background', "#" + this.brushColor);
89
        x = Math.floor($target.prevAll().length % this.options.tiles);
90
        y = Math.floor($target.prevAll().length / this.options.tiles);
91
        return this.drawnTiles.push({
92
          x: x,
93
          y: y,
94
          color: this.brushColor
95
        });
96
      };
97
 
98
      PaintInterface.prototype.downloadImage = function() {
99
        var canvas, context, tile, _i, _len, _ref;
100
        canvas = this.canvasElement[0];
101
        context = canvas.getContext("2d");
102
        context.clearRect(0, 0, this.options.width, this.options.height);
103
        _ref = this.drawnTiles;
104
        for (_i = 0, _len = _ref.length; _i < _len; _i++) {
105
          tile = _ref[_i];
106
          context.fillStyle = tile.color;
107
          context.lineWidth = 0;
108
          context.fillRect(tile.x * this.options.tileSize, tile.y * this.options.tileSize, this.options.tileSize, this.options.tileSize);
109
        }
110
        return window.location.href = canvas.toDataURL("image/png");
111
      };
112
 
113
      PaintInterface.prototype.previewPaper = function(e) {
114
        var tile, _i, _len, _ref, _results;
115
        this.context.clearRect(0, 0, this.options.width, this.options.height);
116
        _ref = this.drawnTiles;
117
        _results = [];
118
        for (_i = 0, _len = _ref.length; _i < _len; _i++) {
119
          tile = _ref[_i];
120
          _results.push(this.drawTile(tile.x, tile.y, tile.color));
121
        }
122
        return _results;
123
      };
124
 
125
      PaintInterface.prototype.clearPaper = function(e) {
126
        return this.paper.find(".tile").css("background-color", "#FFF");
127
      };
128
 
129
      return PaintInterface;
130
 
131
    })();
132
    return new PaintInterface(".colorpit", ".paper", "button.clear", "button.download", {
133
      tiles: 8,
134
      tileSize: 30
135
    });
136
});
 

Pixel Editor

CSSDeck G+