asciiblaster- draw irc art in your web browser |
git clone git://git.acid.vegas/asciiblaster.git |
Log | Files | Refs | Archive | README |
canvas.js (3849B)
1 var canvas = current_canvas = (function(){ 2 3 var cols = 100 4 var rows = 30 5 6 var canvas = new Matrix (cols, rows, function(x,y){ 7 var lex = new Lex (x,y) 8 lex.build() 9 return lex 10 }) 11 12 canvas.bind = function(){ 13 14 canvas.forEach(function(lex, x, y){ 15 16 if (lex.bound) return 17 lex.bound = true 18 var point = [x,y] 19 lex.span.addEventListener('contextmenu', function(e){ 20 e.preventDefault() 21 }) 22 lex.span.addEventListener('mousedown', function(e){ 23 if (is_mobile) return 24 e.preventDefault() 25 dragging = true 26 current_canvas = canvas 27 if (e.altKey) { 28 if (e.shiftKey) { 29 blit.copy_from(canvas, brush, floor(x-brush.w/2), floor(y-brush.h/2)) 30 brush.mask(brush) 31 draw.set_last_point(e, point) 32 } 33 else { 34 brush.load(lex) 35 brush.generate() 36 dragging = false 37 } 38 return 39 } 40 else if (drawing) { 41 undo.new() 42 draw.down(e, lex, point) 43 } 44 else if (selecting) { 45 selection.down(e, lex, point) 46 } 47 else if (transforming) { 48 transform.down(e, lex, point) 49 } 50 else if (filling) { 51 undo.new() 52 draw.fill(brush, x, y) 53 } 54 canvas.focus(x, y) 55 }) 56 57 lex.span.addEventListener("mousemove", function(e){ 58 mouse.x = x 59 mouse.y = y 60 if (is_mobile) return 61 if (! dragging) return 62 if (drawing) { 63 draw.move(e, lex, point) 64 } 65 else if (selecting) { 66 selection.move(e, lex, point) 67 } 68 else if (transforming) { 69 transform.move(e, lex, point) 70 } 71 canvas.focus(x, y) 72 }) 73 74 }) 75 76 if (is_mobile) { 77 canvas.rapper.addEventListener('touchstart', function(e){ 78 e.preventDefault() 79 var x, y, point, lex 80 x = (e.touches[0].pageX - canvas.rapper.offsetTop) / canvas.aa[0][0].span.offsetWidth 81 y = (e.touches[0].pageY - canvas.rapper.offsetTop) / canvas.aa[0][0].span.offsetHeight 82 x = ~~clamp(x, 0, canvas.aa[0].length-1) 83 y = ~~clamp(y, 0, canvas.aa.length-1) 84 point = [x,y] 85 lex = canvas.aa[y][x] 86 dragging = true 87 if (drawing) { 88 undo.new() 89 draw.down(e, lex, point) 90 } 91 else if (filling) { 92 undo.new() 93 draw.fill(brush, x, y) 94 } 95 canvas.focus(x, y) 96 }) 97 canvas.rapper.addEventListener("touchmove", function(e){ 98 e.preventDefault() 99 var x, y, point, lex 100 x = (e.touches[0].pageX - canvas.rapper.offsetTop) / canvas.aa[0][0].span.offsetWidth 101 y = (e.touches[0].pageY - canvas.rapper.offsetTop) / canvas.aa[0][0].span.offsetHeight 102 x = ~~clamp(x, 0, canvas.aa[0].length-1) 103 y = ~~clamp(y, 0, canvas.aa.length-1) 104 point = [x,y] 105 lex = canvas.aa[y][x] 106 if (! dragging) return 107 shader_el.innerHTML = point.join(",") 108 if (drawing) { 109 draw.move(e, lex, point) 110 } 111 canvas.focus(x, y) 112 }) 113 } 114 115 } 116 117 canvas.min = 1 118 canvas.max = 999 119 120 // canvas.resize(1, 1, true) // wont create undo state 121 canvas.resize = function(w, h, no_undo){ 122 var old_w = this.w, old_h = this.h 123 w = this.w = clamp(w, this.min, this.max) 124 h = this.h = clamp(h, this.min, this.max) 125 if (old_w === w && old_h === h) return; 126 127 if (!no_undo){ 128 undo.new() 129 undo.save_resize(w, h, old_w, old_h) 130 } 131 132 canvas.__proto__.resize.call(canvas, w, h) 133 controls.canvas_w.char = "" + w 134 controls.canvas_w.build() 135 controls.canvas_h.char = "" + h 136 controls.canvas_h.build() 137 } 138 canvas.size_add = function(w, h){ 139 canvas.resize(canvas.w + w, canvas.h + h) 140 } 141 142 return canvas 143 144 })()