-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutils.js
More file actions
44 lines (35 loc) · 926 Bytes
/
utils.js
File metadata and controls
44 lines (35 loc) · 926 Bytes
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
function getMousePos(canvas, evt) {
var rect = canvas.getBoundingClientRect();
return {
x: (evt.clientX - rect.left) * (canvas.width / rect.width),
y: (evt.clientY - rect.top) * (canvas.height / rect.height),
};
}
function lerp(a, b, s) {
return a*(1-s) + b*s;
}
var CP = window.CanvasRenderingContext2D && CanvasRenderingContext2D.prototype;
if (CP && CP.lineTo){
CP.dashedLine = function(x,y,x2,y2,dashArray){
var len = Math.sqrt( Math.pow(x-x2, 2) + Math.pow(y-y2, 2) );
var dash = dashArray[0] / len;
var space = dashArray[1] / len;
var ran = 0;
var isDash = false;
while(ran < 1) {
if(isDash)
ran += dash;
else
ran += space;
if(ran > 1)
ran = 1;
var x1 = lerp(x,x2, ran);
var y1 = lerp(y,y2, ran);
if(isDash)
this.lineTo(x1,y1);
else
this.moveTo(x1,y1)
isDash = !isDash;
}
}
}