-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
129 lines (107 loc) · 3.26 KB
/
main.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
var canvasDiv = document.getElementById('canva-container');
var canvas = document.createElement('canvas');
canvas.setAttribute('id', 'drawCanvas');
canvasDiv.appendChild(canvas);
if (typeof G_vmlCanvasManager != 'undefined') {
canvas = G_vmlCanvasManager.initElement(canvas);
}
var context = canvas.getContext('2d');
var canvaDraw = document.getElementById("drawCanvas");
initialize();
function initialize() {
window.addEventListener('resize', resizeCanvas, false);
resizeCanvas();
}
function redrawCanvas(w, h) {
context.strokeStyle = 'black';
context.lineWidth = '5';
context.strokeRect(0, 0, w, h);
}
function resizeCanvas() {
canvaDraw.width = document.getElementById("canva-container").clientWidth * 0.9;
canvaDraw.height = document.getElementById("canva-container").clientHeight;
redrawCanvas(canvaDraw.width, canvaDraw.height);
}
function clearCanvas() {
context.clearRect(0, 0, canvaDraw.width, canvaDraw.height);
resizeCanvas(canvaDraw.width, canvaDraw.height);
emptycoordinates();
}
function emptycoordinates() {
clickX = [];
clickY = [];
clickDrag = [];
}
//Draw
var clickX = new Array();
var clickY = new Array();
var clickDrag = new Array();
var adjusty = document.getElementById("canva-container").clientHeight * 0.20;
var paint;
function addClick(x, y, dragging) {
clickX.push(x);
clickY.push(y);
clickDrag.push(dragging);
}
$('#drawCanvas').bind("vmousedown ", function (e) {
e.preventDefault();
var mouseX = e.pageX - this.offsetLeft;
var mouseY = e.pageY - adjusty;
paint = true;
addClick(mouseX, mouseY);
redraw();
});
$('#drawCanvas').bind("vmousemove swipe", function (e) {
e.preventDefault();
if (paint) {
addClick(e.pageX - this.offsetLeft, e.pageY - adjusty - this.offsetTop, true);
redraw();
}
});
$('#drawCanvas').bind("vmouseup ", function (e) {
e.preventDefault();
paint = false;
});
$('#drawCanvas').bind("vmousecancel", function (e) {
e.preventDefault();
paint = false;
});
function palette() {
colors = ["#000000", "#f40919", "#f408a1", "#f408a1", "#a108f4", "#a108f4", "#f4f408", "#f40b08", "#08f4d0", "#08f4d0"];
for (var i = 0; i < 10; i++) {
document.getElementById("colorstream").innerHTML += "<span class='color-select'></span>";
document.getElementsByClassName("color-select")[i].style.backgroundColor = colors[i];
}
}
palette();
var linecolor;
var setcolor = document.querySelectorAll(".color-select");
for (i = 0; i < setcolor.length; i++) {
setcolor[i].addEventListener("click", function () {
linecolor = this.style.backgroundColor;
emptycoordinates();
});
}
$("#palette").change(function () {
linecolor = this.value;
emptycoordinates();
});
function redraw() {
context.lineJoin = "round";
context.lineWidth = 5;
for (var i = 0; i < clickX.length; i++) {
context.beginPath();
if (clickDrag[i] && i) {
context.moveTo(clickX[i - 1], clickY[i - 1]);
} else {
context.moveTo(clickX[i] - 1, clickY[i]);
}
context.lineTo(clickX[i], clickY[i]);
context.closePath();
context.strokeStyle = linecolor;
context.stroke();
}
}
$("#clear").click(function () {
clearCanvas();
});