-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
108 lines (79 loc) · 2.16 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
var c = document.getElementById('c');
c.width = 800;
c.height = 600;
var ctx = c.getContext('2d'),
cw = c.width,
ch = c.height;
//default values
var color = '#4dc4a8';
var shape = 'circle';
var bgcolor = '#fff';
var radius = 40;
ctx.fillStyle = bgcolor;
ctx.fillRect(0,0, c.width, c.height);
function drawCircles(x, y){
ctx.beginPath();
ctx.arc(x, y, radius, 0, 2*Math.PI);
ctx.closePath();
ctx.fillStyle = bgcolor;
ctx.fill();
ctx.lineWidth = 1;
ctx.strokeStyle = color;
ctx.stroke();
}
function drawHex(x, y){
ctx.beginPath();
// var side = 0,
// size = 40,
// x = 100,
// y = 100;
// ctx.moveTo(x + size * Math.cos(0), y + size * Math.sin(0));
// for (side; side < 7; side++) {
// ctx.lineTo(x + size * Math.cos(side * 2 * Math.PI / 6), y + size * Math.sin(side * 2 * Math.PI / 6));
// }
// ctx.strokeStyle = color;
// ctx.stroke();
ctx.beginPath();
ctx.moveTo(x,y);
ctx.lineTo(350,150);
ctx.strokeStyle = color;
ctx.stroke();
}
c.addEventListener('mousemove', function(evt){
var rect = c.getBoundingClientRect();
switch(shape){
case 'circle':
drawCircles(evt.clientX - rect.left, evt.clientY - rect.top);
break;
case 'hexagon':
drawHex(evt.clientX - rect.left, evt.clientY - rect.top);
break;
default:
drawCircles(evt.clientX - rect.left, evt.clientY - rect.top);
}
});
var colorpicker = document.getElementById('color');
colorpicker.addEventListener('change', function(){
color = colorpicker.value;
});
// var shape = document.getElementById('shape');
// shape.addEventListener('click', function(){
// shape = 'hexagon';
// });
var bgpicker = document.getElementById('bgcolor');
bgpicker.addEventListener('change', function(){
bgcolor = bgpicker.value;
ctx.fillStyle = bgcolor;
ctx.fillRect(0,0, c.width, c.height);
})
var save = document.getElementById('save');
save.addEventListener('click', function(){
dataURI = c.toDataURL('');
var url = dataURI.replace(/^data:image\/[^;]+/, 'data:application/octet-stream');
window.open(url);
// window.location.href = 'data:application/octet-stream;base64,' + dataURI;
})
var size = document.getElementById('size');
size.addEventListener('change', function(){
radius = size.value;
})