-
Notifications
You must be signed in to change notification settings - Fork 0
/
drawing.js
54 lines (42 loc) · 1.56 KB
/
drawing.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
var page1 = document.getElementById('page1');
var canvas_element = document.createElement('canvas');
var ctx = canvas_element.getContext('2d');
canvas_element.id = 'canvas_drawing';
canvas_element.style.display = 'none';
page1.appendChild(canvas_element);
var page1Rect = page1.getBoundingClientRect();
canvas_element.width = page1Rect.width;
canvas_element.height = page1Rect.height;
ctx.lineWidth = 1;
ctx.lineJoin = 'round';
ctx.lineCap = 'round';
ctx.strokeStyle = '#00CC99';
var mouse = {x: 0, y: 0};
var btn_draw = document.getElementById('btn_draw')
btn_draw.addEventListener('change', function(ev){
if(ev.target.checked){
canvas_element.style.display = 'block';
} else {
canvas_element.style.display = 'none';
}
})
canvas_element.addEventListener('mousedown', function(e) {
ctx.beginPath();
ctx.moveTo(mouse.x, mouse.y);
canvas_element.addEventListener('mousemove', onPaint, false);
}, false);
canvas_element.addEventListener('mouseup', function() {
canvas_element.removeEventListener('mousemove', onPaint, false);
}, false);
canvas_element.addEventListener('mousemove', function(e) {
var targetParentRect = e.target.parentElement.getBoundingClientRect();
mouse.x = (e.pageX - targetParentRect.x - this.offsetLeft)/scale;
mouse.y = (e.pageY - targetParentRect.y - this.offsetTop)/scale;
console.log('parent x: ' + targetParentRect.x + 'offsetLeft: ' + this.offsetLeft)
}, false);
var onPaint = function() {
console.log('drawing')
console.log(mouse)
ctx.lineTo(mouse.x, mouse.y);
ctx.stroke();
};