-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
122 lines (101 loc) · 3.2 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
$(document).ready(function() {
let canvas = document.getElementById("canvas");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
let context = canvas.getContext("2d");
context.fillStyle = "white";
context.fillRect(0, 0, canvas.width, canvas.height);
let restore_array = [];
let start_index = -1;
let stroke_color = 'black';
let stroke_width = "1";
let is_drawing = false;
$(document).on('input', '#stroke-input', function() {
stroke_width = $(this).val();
});
function start(event) {
is_drawing = true;
context.beginPath();
context.moveTo(getX(event), getY(event));
event.preventDefault();
}
function draw(event) {
if (is_drawing) {
context.lineTo(getX(event), getY(event));
context.strokeStyle = stroke_color;
context.lineWidth = stroke_width;
context.lineCap = "round";
context.lineJoin = "round";
context.stroke();
}
event.preventDefault();
}
function stop(event) {
if (is_drawing) {
context.stroke();
context.closePath();
is_drawing = false;
}
event.preventDefault();
restore_array.push(context.getImageData(0, 0, canvas.width, canvas.height));
start_index += 1;
}
function getX(event) {
if (event.pageX == undefined) {return event.targetTouches[0].pageX - canvas.offsetLeft}
else {return event.pageX - canvas.offsetLeft}
}
function getY(event) {
if (event.pageY == undefined) {return event.targetTouches[0].pageY - canvas.offsetTop}
else {return event.pageY - canvas.offsetTop}
}
canvas.addEventListener("touchstart", start, false);
canvas.addEventListener("touchmove", draw, false);
canvas.addEventListener("touchend", stop, false);
canvas.addEventListener("mousedown", start, false);
canvas.addEventListener("mousemove", draw, false);
canvas.addEventListener("mouseup", stop, false);
canvas.addEventListener("mouseout", stop, false);
function download(url){
var a = $("<a style='display:none'>")
.attr("href", url)
.attr("download", "canvas-draving.png")
.appendTo("body");
a[0].click();
a.remove();
}
function saveCapture(element) {
html2canvas(element).then(function(canvas) {
download(canvas.toDataURL("image/png"));
})
}
$('.download-button').click(function(){
var element = document.querySelector(".canvas-holder");
saveCapture(element)
})
// add page-loaded class when page loads
$("body").addClass("page-loaded")
// remove page-loaded class and close popup
$(".popup svg").click(function(){
$("body").removeClass("page-loaded");
$(".popup").hide();
})
// open aside tool when clicked on it
$(".aside-menu .button svg.tool-icon").click(function(){
$(this).parent().addClass("active")
})
// close aside tool using close button
$(".aside-menu .button svg.aside-close").click(function(){
$(".aside-menu .button").removeClass("active")
})
// open color picker
$("input[type=color]").change(function(e){
stroke_color = $(this).val();
});
// reset canvas
function resetCanvas() {
context.clearRect(0, 0, context.canvas.width, context.canvas.height);
}
$(".aside-menu .reset-button").click(function(){
resetCanvas();
})
});