forked from ivlup/Cell-Simulator
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcanvas.js
85 lines (72 loc) · 2.23 KB
/
canvas.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
const canvas = document.querySelector("canvas")
canvas.ontouchstart = (e)=>{e.preventDefault()}
const ctx = canvas.getContext('2d', { willReadFrequently: true });
if (window.devicePixelRatio > 1) {
var canvasWidth = canvas.width;
var canvasHeight = canvas.height;
canvas.width = canvasWidth * window.devicePixelRatio;
canvas.height = canvasHeight * window.devicePixelRatio;
canvas.style.width = canvasWidth + "px";
canvas.style.height = canvasHeight + "px";
ctx.scale(window.devicePixelRatio, window.devicePixelRatio);
}
const width = canvas.width/window.devicePixelRatio
const height = canvas.height/window.devicePixelRatio
ctx.translate(width/2, height/2)
function circle(v, radius, fill, fillStyle, stroke, strokeWidth) {
ctx.beginPath()
ctx.arc(v.x, v.y, radius, 0, 2 * Math.PI, false)
ctx.closePath()
if (fill) {
ctx.fillStyle = fill
ctx.fill()
}
if (stroke) {
ctx.lineWidth = strokeWidth
ctx.strokeStyle = stroke
ctx.stroke()
}
}
function line(v1,v2, strokeWidth){
ctx.beginPath();
ctx.moveTo(v1.x, v1.y);
ctx.lineTo(v2.x, v2.y);
// Draw the Path
ctx.closePath()
ctx.lineWidth = strokeWidth
ctx.stroke();
}
function polygon(vs, fill, fillStyle, stroke, strokeWidth){
ctx.beginPath()
ctx.moveTo(vs[0].x, vs[0].y)
for(let i = 1; i<vs.length; i++){
ctx.lineTo(vs[i].x, vs[i].y)
}
ctx.closePath()
if (fill) {
ctx.fillStyle = fillStyle
ctx.fill()
}
if (stroke) {
ctx.lineWidth = strokeWidth
ctx.strokeStyle = stroke
ctx.stroke()
}
}
function arrow(v1, v2,color) {
var headlen = 2; // length of head in pixels
var dx = v2.x - v1.x;
var dy = v2.y - v1.y;
var angle = Math.atan2(dy, dx);
ctx.lineWidth = 1
ctx.strokeStyle = color ?? "#0000"
ctx.beginPath();
ctx.moveTo(v1.x, v1.y);
ctx.lineTo(v2.x, v2.y);
ctx.lineTo(v2.x - headlen * Math.cos(angle - Math.PI / 6), v2.y - headlen * Math.sin(angle - Math.PI / 6));
ctx.moveTo(v2.x, v2.y);
ctx.lineTo(v2.x - headlen * Math.cos(angle + Math.PI / 6), v2.y - headlen * Math.sin(angle + Math.PI / 6));
ctx.closePath();
ctx.stroke();
ctx.strokeStyle = 'black'
}