summaryrefslogtreecommitdiff
path: root/public/js/draw.js
blob: 5cc4163ae8a9bb5535fb67bd5edd80e79b1cb80a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
var drawing = false;
var workspace = document.getElementById("workspace");
workspace.width = window.innerWidth * 0.9;
workspace.height = window.innerHeight;
workspace.id = "workspace";
var workspaceCtx = workspace.getContext('2d');
var lastpoint;

workspace.onmousedown = function(e){
  drawing = true;
}
document.onmousemove = function(e){
  if (drawing) {
    newpoint = new Point(e);
    if (lastpoint) {
      draw(lastpoint, newpoint);
    }
    lastpoint = newpoint;
  }
}
window.onmouseup = function(){
  if (drawing) {
    drawing = false;
    lastpoint = null;
  }
}
function draw(start, end){
  var halfBrushW = brush.width/2;
  var halfBrushH = brush.height/2;
 
  var distance = parseInt( Trig.distanceBetween2Points( start, end ) );
  var angle = Trig.angleBetween2Points( start, end );
 
  for ( var z=0; (z<=distance || z==0); z += 2 ) {
    var x = start.x + (Math.sin(angle) * z) - halfBrushW;
    var y = start.y + (Math.cos(angle) * z) - halfBrushH;
    workspaceCtx.drawImage(brush.canvas, x, y);
  }
}

function Point(e) {
  this.x = e.pageX - workspace.offsetLeft;
  this.y = e.pageY - workspace.offsetTop;
}