-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcanvas.js
73 lines (66 loc) · 1.98 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
const CONTEXT_2D = '2d';
const CONTEXT_WEBGL = 'webgl';
const EXPERIMENTAL_WEBGL = 'experimental-webgl';
if(helper){
//ok
}else{
window.helper={};
}
//canvas context is 2D;
helper.Canvas2D = {
getCanvas:function (selector) {
const canvas = document.querySelector(selector);
return canvas;
},
getContext : function(canvas){
return canvas.getContext(CONTEXT_2D);
},
setSize : function(canvas, height, width){
canvas.height = height;
canvas.width = width;
},
drawLine: function(canvas, x1, y1, x2, y2, color,lineWidth=3) {
//get context
let ctx = helper.Canvas2D.getContext(canvas);
ctx.beginPath();
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
ctx.fill();
ctx.strokeStyle = color;
ctx.lineWidth = lineWidth;
ctx.stroke();
},
drawRectange: function(canvas, x1, y1, x2, y2, color){
const ctx = helper.Canvas2D.getContext(canvas);
ctx.fillStyle = color;
ctx.fillRect(x1, y1, x2, y2);
},
pointToPointBeizerCurve: function(canvas, x1, y1, x2, y2, color, lineWidth=3){
let ctx = helper.Canvas2D.getContext(canvas);
ctx.moveTo(x1,y1);
ctx.bezierCurveTo(50,90,159,-30, x2,y2);
ctx.lineWidth = lineWidth;
ctx.strokeStyle = color;
ctx.stroke();
}
};
//canvas context is 3D;
helper.Canvas3D = {
getCanvas:function (selector) {
const canvas = document.querySelector(selector);
return canvas;
},
getContext : function(canvas){
return canvas.getContext(CONTEXT_3D);
},
initializeWebGL : function() {
var webgl = canvas.getContext(CONTEXT_WEBGL)
|| canvas.getContext(EXPERIMENTAL_WEBGL);
if (!webgl || !(webgl instanceof WebGLRenderingContext) ) {
alert('Failed to get WebGL context.');
} else {
alert('Great, your browser supports WebGL.');
}
return webgl;
},
}