From 33592730dd9641935e5026dcd7ee0eb8efd85144 Mon Sep 17 00:00:00 2001 From: Devine Lu Linvega Date: Fri, 11 Nov 2016 16:00:41 -0800 Subject: [PATCH] Starting to clean up the project. --- index.html | 2 ++ scripts/main.js | 39 +++++---------------------------------- scripts/pointer.js | 10 ++++++++-- 3 files changed, 15 insertions(+), 36 deletions(-) diff --git a/index.html b/index.html index 9be0cd4..cf32526 100644 --- a/index.html +++ b/index.html @@ -1,10 +1,12 @@ + + \ No newline at end of file diff --git a/scripts/main.js b/scripts/main.js index 1274702..3cce0d4 100644 --- a/scripts/main.js +++ b/scripts/main.js @@ -2,43 +2,14 @@ var pointer = new Pointer(); var canvas = document.getElementById('myCanvas'); var context = canvas.getContext('2d'); -function writeMessage(canvas, message) { - var context = canvas.getContext('2d'); - // context.clearRect(0, 0, canvas.width, canvas.height); - context.font = '12pt Calibri'; - context.fillStyle = 'black'; - context.fillText(message, 10, 25); -} - -function getMousePos(canvas, evt) -{ - var rect = canvas.getBoundingClientRect(); - return new Position(evt.clientX - rect.left,evt.clientY - rect.top); -} - -canvas.addEventListener('mousemove', function(evt) { - pointer.position = getMousePos(canvas, evt); - pointer.draw(); - // var message = 'Mouse position: ' + mousePos.x + ',' + mousePos.y; - // writeMessage(canvas, message); - // draw_pixel(context,mousePos.x,mousePos.y); +canvas.addEventListener('mousemove', function(e) { + pointer.draw(e); }, false); -canvas.addEventListener('mousedown', function(evt) { +canvas.addEventListener('mousedown', function(e) { pointer.can_draw = true; }, false); -canvas.addEventListener('mouseup', function(evt) { +canvas.addEventListener('mouseup', function(e) { pointer.can_draw = false; -}, false); - -function draw_pixel(context,x,y) -{ - var id = context.createImageData(1,1); - var d = id.data; - d[0] = 0; - d[1] = 0; - d[2] = 0; - d[3] = 255; - context.putImageData(id,x,y); -} \ No newline at end of file +}, false); \ No newline at end of file diff --git a/scripts/pointer.js b/scripts/pointer.js index b51b651..fe3890d 100644 --- a/scripts/pointer.js +++ b/scripts/pointer.js @@ -3,7 +3,7 @@ function Pointer() this.position = new Position(); this.can_draw = false; - this.draw = function() + this.draw = function(e) { if(this.can_draw === false){return;} @@ -13,6 +13,12 @@ function Pointer() d[1] = 0; d[2] = 0; d[3] = 255; - context.putImageData(id,this.position.x,this.position.y); + context.putImageData(id,e.clientX,e.clientY); + } + + this.position = function() + { + var rect = canvas.getBoundingClientRect(); + return new Position(evt.clientX - rect.left,evt.clientY - rect.top); } } \ No newline at end of file