diff --git a/index.html b/index.html index cf32526..1424a23 100644 --- a/index.html +++ b/index.html @@ -3,9 +3,10 @@ + - + diff --git a/scripts/main.js b/scripts/main.js index 3cce0d4..4770fda 100644 --- a/scripts/main.js +++ b/scripts/main.js @@ -1,15 +1,26 @@ -var pointer = new Pointer(); + var canvas = document.getElementById('myCanvas'); var context = canvas.getContext('2d'); +var brush = new Brush(); canvas.addEventListener('mousemove', function(e) { - pointer.draw(e); + brush.draw(e); }, false); canvas.addEventListener('mousedown', function(e) { - pointer.can_draw = true; + brush.draw_start(e); }, false); canvas.addEventListener('mouseup', function(e) { - pointer.can_draw = false; -}, false); \ No newline at end of file + brush.draw_stop(e); +}, false); + +var mirror_test = new Pointer(); +mirror_test.mirror = new Position(200,0); +brush.add_pointer(mirror_test); + +var mirror_test2 = new Pointer(new Position(0,10)); +mirror_test2.mirror = new Position(200,0); +brush.add_pointer(mirror_test2); + +brush.add_pointer(new Pointer(new Position(0,10))); \ No newline at end of file diff --git a/scripts/pointer.js b/scripts/pointer.js index fe3890d..6a9d82a 100644 --- a/scripts/pointer.js +++ b/scripts/pointer.js @@ -1,11 +1,12 @@ -function Pointer() +function Pointer(offset = new Position()) { - this.position = new Position(); - this.can_draw = false; + this.offset = offset; + this.mirror = null; + this.position_prev = null; - this.draw = function(e) + this.draw = function() { - if(this.can_draw === false){return;} + if(!this.position_prev){this.position_prev = this.position(); } var id = context.createImageData(1,1); var d = id.data; @@ -13,12 +14,30 @@ function Pointer() d[1] = 0; d[2] = 0; d[3] = 255; - context.putImageData(id,e.clientX,e.clientY); + context.putImageData(id,this.position().x,this.position().y); + + context.beginPath(); + context.moveTo(this.position_prev.x,this.position_prev.y); + context.lineTo(this.position().x,this.position().y); + context.stroke(); + + this.position_prev = this.position(); } this.position = function() { - var rect = canvas.getBoundingClientRect(); - return new Position(evt.clientX - rect.left,evt.clientY - rect.top); + if(this.mirror){ + return new Position(500 - (brush.position.x + this.offset.x), brush.position.y + this.offset.y); + } + return new Position(brush.position.x + this.offset.x, brush.position.y + this.offset.y); + } + + this.start = function() + { + } + + this.stop = function() + { + this.position_prev = null; } } \ No newline at end of file diff --git a/scripts/position.js b/scripts/position.js index ca63c12..69c5910 100644 --- a/scripts/position.js +++ b/scripts/position.js @@ -2,4 +2,9 @@ function Position(x = 0,y = 0) { this.x = x; this.y = y; + + this.distance_to = function(target) + { + return Math.sqrt( (this.x-target.x)*(this.x-target.x) + (this.y-target.y)*(this.y-target.y) ); + } } \ No newline at end of file