summaryrefslogtreecommitdiff
path: root/js/ui/canvas.js
diff options
context:
space:
mode:
authortimb <opuscule@gmail.com>2015-07-22 00:25:07 -0700
committertimb <opuscule@gmail.com>2015-07-22 00:25:07 -0700
commita76225dc7f8ec707fc2dc563a796c65620b1c5a4 (patch)
tree313932c110e782565224b9ae6be53f39a6325677 /js/ui/canvas.js
parent1a009fd877364f5c6616a381d39e33902201a298 (diff)
finally hook up undo
Diffstat (limited to 'js/ui/canvas.js')
-rw-r--r--js/ui/canvas.js71
1 files changed, 50 insertions, 21 deletions
diff --git a/js/ui/canvas.js b/js/ui/canvas.js
index 4822527..e241fd1 100644
--- a/js/ui/canvas.js
+++ b/js/ui/canvas.js
@@ -3,15 +3,15 @@ var canvas = current_canvas = (function(){
var cols = 100
var rows = 30
- var exports = new Matrix (cols, rows, function(x,y){
+ var canvas = new Matrix (cols, rows, function(x,y){
var lex = new Lex (x,y)
lex.build()
return lex
})
- exports.bind = function(){
+ canvas.bind = function(){
- exports.forEach(function(lex, x, y){
+ canvas.forEach(function(lex, x, y){
if (lex.bound) return
lex.bound = true
@@ -27,7 +27,7 @@ var canvas = current_canvas = (function(){
if (e.altKey) {
if (e.shiftKey) {
blit.copy_from(canvas, brush, floor(x-brush.w/2), floor(y-brush.h/2))
- brush.mask(brush)
+ brush.mask(brush)
draw.set_last_point(e, point)
}
else {
@@ -38,15 +38,17 @@ var canvas = current_canvas = (function(){
return
}
else if (drawing) {
+ undo.new()
draw.down(e, lex, point)
}
else if (selecting) {
selection.down(e, lex, point)
}
else if (filling) {
+ undo.new()
draw.fill(brush, x, y)
}
- lex.focus()
+ canvas.focus(x, y)
})
lex.span.addEventListener("mousemove", function(e){
@@ -58,50 +60,77 @@ var canvas = current_canvas = (function(){
else if (selecting) {
selection.move(e, lex, point)
}
- lex.focus()
+ canvas.focus(x, y)
})
})
if (is_mobile) {
- exports.rapper.addEventListener('touchstart', function(e){
+ canvas.rapper.addEventListener('touchstart', function(e){
e.preventDefault()
var x, y, point, lex
- x = (e.touches[0].pageX - exports.rapper.offsetTop) / exports.aa[0][0].span.offsetWidth
- y = (e.touches[0].pageY - exports.rapper.offsetTop) / exports.aa[0][0].span.offsetHeight
- x = ~~clamp(x, 0, exports.aa[0].length-1)
- y = ~~clamp(y, 0, exports.aa.length-1)
+ x = (e.touches[0].pageX - canvas.rapper.offsetTop) / canvas.aa[0][0].span.offsetWidth
+ y = (e.touches[0].pageY - canvas.rapper.offsetTop) / canvas.aa[0][0].span.offsetHeight
+ x = ~~clamp(x, 0, canvas.aa[0].length-1)
+ y = ~~clamp(y, 0, canvas.aa.length-1)
point = [x,y]
- lex = exports.aa[y][x]
+ lex = canvas.aa[y][x]
dragging = true
if (drawing) {
+ undo.new()
draw.down(e, lex, point)
}
else if (filling) {
+ undo.new()
draw.fill(brush, x, y)
}
- lex.focus()
+ canvas.focus(x, y)
})
- exports.rapper.addEventListener("touchmove", function(e){
+ canvas.rapper.addEventListener("touchmove", function(e){
e.preventDefault()
var x, y, point, lex
- x = (e.touches[0].pageX - exports.rapper.offsetTop) / exports.aa[0][0].span.offsetWidth
- y = (e.touches[0].pageY - exports.rapper.offsetTop) / exports.aa[0][0].span.offsetHeight
- x = ~~clamp(x, 0, exports.aa[0].length-1)
- y = ~~clamp(y, 0, exports.aa.length-1)
+ x = (e.touches[0].pageX - canvas.rapper.offsetTop) / canvas.aa[0][0].span.offsetWidth
+ y = (e.touches[0].pageY - canvas.rapper.offsetTop) / canvas.aa[0][0].span.offsetHeight
+ x = ~~clamp(x, 0, canvas.aa[0].length-1)
+ y = ~~clamp(y, 0, canvas.aa.length-1)
point = [x,y]
- lex = exports.aa[y][x]
+ lex = canvas.aa[y][x]
if (! dragging) return
shader_el.innerHTML = point.join(",")
if (drawing) {
draw.move(e, lex, point)
}
- lex.focus()
+ canvas.focus(x, y)
})
}
}
- return exports
+ canvas.min = 1
+ canvas.max = 999
+
+ // canvas.resize(1, 1, true) // wont create undo state
+ canvas.resize = function(w, h, no_undo){
+ var old_w = this.w, old_h = this.h
+ w = this.w = clamp(w, this.min, this.max)
+ h = this.h = clamp(h, this.min, this.max)
+ if (old_w === w && old_h === h) return;
+
+ if (!no_undo){
+ undo.new()
+ undo.save_resize(w, h, old_w, old_h)
+ }
+
+ canvas.__proto__.resize.call(canvas, w, h)
+ controls.canvas_w.char = "" + w
+ controls.canvas_w.build()
+ controls.canvas_h.char = "" + h
+ controls.canvas_h.build()
+ }
+ canvas.size_add = function(w, h){
+ canvas.resize(canvas.w + w, canvas.h + h)
+ }
+
+ return canvas
})()