-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
68 lines (59 loc) · 1.45 KB
/
script.js
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
var curve = new Bezier();
var selected = null;
var dragged = false;
var canvas=document.getElementById("canvas");
canvas.width = 640;
canvas.height = 480;
var ctx=canvas.getContext("2d");
var rect = canvas.getBoundingClientRect();
canvas.addEventListener('click', onMouseClick);
canvas.addEventListener('mousedown', onMouseDown);
canvas.addEventListener('mouseup', onMouseUp);
canvas.addEventListener('mousemove', onMouseMove);
function getMousePos(e){
var x = e.pageX - canvas.offsetLeft;
var y = e.pageY - canvas.offsetTop;
return new Point(x,y);
}
function onMouseClick(e){
var p = getMousePos(e);
var addPoint = true;
for(var i in curve.cPoints){
if(curve.cPoints[i].equal(p)){
curve.cPoints[i].active = true;
addPoint = false;
}else{
curve.cPoints[i].active = false;
}
}
if(addPoint){
p.active = true;
curve.cPoints.push(p)
};
redraw();
}
function onMouseDown(e){
dragged = true;
var pos = getMousePos(e);
selected = curve.updateActivePoint(pos);
}
function onMouseMove(e){
if (!dragged) {
return;
}
p = getMousePos(e);
selected.x = p.x;
selected.y = p.y;
redraw();
}
function onMouseUp(e){
dragged = false;
}
function redraw(){
ctx.clear("#FFF");
curve.draw(ctx,10);
}
ctx.clear = function(fillColor) {
ctx.fillStyle = fillColor;
ctx.fillRect(0,0,rect.width, rect.height);
};