-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo2.js
78 lines (72 loc) · 1.67 KB
/
demo2.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
window.onload=function(){
var yyy = document.getElementById('canvas');
var context = yyy.getContext('2d');
autoSiteCanvasSize(yyy)
listenToMouse(yyy)
var eraserEnabled = false;
eraser.onclick = function(){
eraserEnabled = true;
actions.className = 'actions x'
}
brush.onclick = function(){
eraserEnabled = false;
actions.className = 'actions'
}
/********/
function autoSiteCanvasSize(canvas){
siteCanvasSize()
window.onresize = function(){
siteCanvasSize()
}
function siteCanvasSize(){
var pageWidth = document.documentElement.clientWidth;
var pageHeight = document.documentElement.clientHeight;
canvas.width = pageWidth;
canvas.height = pageHeight;
}
}
function drawCircle(x,y,radius){
context.beginPath()
context.arc(x,y,radius,0,Math.PI*2);
context.fill()
}
function drawLine(x1,y1,x2,y2){
context.beginPath();
context.moveTo(x1,y1);
context.lineWidth = 6;
context.lineTo(x2,y2);
context.stroke();
context.closePath();
}
function listenToMouse(canvas){
var using = false;
var lastPoint = {x:undefined, y:undefined}
canvas.onmousedown = function(aaa){
var x = aaa.clientX;
var y = aaa.clientY;
using = true;
if(eraserEnabled){
context.clearRect(x-5,y-5,10,10)
}else{
lastPoint = {x:x,y:y}
drawCircle(x,y,3)
}
}
canvas.onmousemove = function(aaa){
var x = aaa.clientX;
var y = aaa.clientY;
if(!using){return}
if(eraserEnabled){
context.clearRect(x-5,y-5,10,10)
}else{
var newPoint = {x:x,y:y}
drawCircle(x,y,3)
drawLine(lastPoint.x,lastPoint.y,newPoint.x,newPoint.y);
lastPoint = newPoint;
}
}
canvas.onmouseup = function(){
using = false;
}
}
}