-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
68 lines (56 loc) · 1.81 KB
/
index.js
File metadata and controls
68 lines (56 loc) · 1.81 KB
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
window.onload = init;
function init(){
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var colorBtn = document.getElementById("colorBtn");
var strokeSizeSlider = document.getElementById("strokeSizer");
var strokeSizeTxt = document.getElementById("strokeSizeTxt")
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
//Code Snippet from StackOverflow user K3N:
//https://stackoverflow.com/questions/17130395/real-mouse-position-in-canvas
function getMousePos(canvas, evt) {
var rect = canvas.getBoundingClientRect(), // abs. size of element
scaleX = canvas.width / rect.width, // relationship bitmap vs. element for X
scaleY = canvas.height / rect.height; // relationship bitmap vs. element for Y
return {
x: (evt.clientX - rect.left) * scaleX, // scale mouse coordinates after they have
y: (evt.clientY - rect.top) * scaleY // been adjusted to be relative to element
}
}
strokeSizeSlider.oninput = function sliderChangeListener(e){
strokeSizeTxt.value = this.value;
ctx.lineWidth = this.value;
}
strokeSizeTxt.onchange = function sliderTxtChangeListener(e){
strokeSizeSlider.value = this.value;
ctx.lineWidth = this.value;
}
penDown = false;
canvas.onmousedown = function mouseDownListener(e){
penDown = true;
ctx.beginPath();
}
canvas.onmouseup = function mouseUpListener(e){
penDown = false;
ctx.closePath();
}
canvas.onmouseout = function mouseOutListener(e){
penDown = false;
ctx.closePath();
}
canvas.onmousemove = function mouseMoveListener(e){
var pos = getMousePos(canvas,e);
if(penDown){
ctx.lineTo(pos.x,pos.y);
ctx.stroke();
}
else{
ctx.moveTo(pos.x,pos.y);
}
}
colorBtn.onchange = function colorChangeListener(e){
console.log(this.jscolor);
ctx.strokeStyle = "#" + this.jscolor;
}
}