forked from ayush8010720467/web_whiteboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
85 lines (81 loc) · 2.37 KB
/
index.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
context = document.getElementById('white_board').getContext("2d");
wb = document.getElementById('white_board')
wb.height = window.innerHeight;
wb.width = window.innerWidth;
$('#white_board').mousedown(function (e) {
var mouseX = e.pageX - this.offsetLeft;
var mouseY = e.pageY - this.offsetTop;
paint = true;
addClick(e.pageX - this.offsetLeft, e.pageY - this.offsetTop);
redraw();
});
$('#white_board').mousemove(function (e) {
if (paint) {
addClick(e.pageX - this.offsetLeft, e.pageY - this.offsetTop, true);
redraw();
}
});
$('#white_board').mouseup(function (e) {
paint = false;
});
$('#white_board').mouseleave(function (e) {
paint = false;
});
var clickX = new Array();
var clickY = new Array();
var clickDrag = new Array();
var colorWhite = "#ffffff";
var curColor = colorWhite;
var clickColor = new Array();
var paint;
function addClick(x, y, dragging) {
clickX.push(x);
clickY.push(y);
clickDrag.push(dragging);
clickColor.push(curColor);
}
function redraw() {
context.clearRect(0, 0, context.canvas.width, context.canvas.height); // Clears the canvas
context.fillStyle = '#000'
context.fillRect(0,0,wb.width,wb.height)
// context.strokeStyle = "#df4b26";
context.lineJoin = "round";
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 = clickColor[i]
// context.lineWidth = radius; // change the size of the stroke
context.stroke();
}
}
$('#clear_complete').click(function () {
let canvas = document.getElementById('white_board')
clickX = [];
clickY = [];
clickDrag = [];
context.fillStyle = '#000'
context.fillRect(0,0,wb.width,wb.height)
});
function saveImage(){
var image = wb.toDataURL()
aLink = document.getElementById('test')
aLink.download = 'web_whiteboard.png'
aLink.href = image;
console.log('done')
aLink.click()
}
function createPage(){
var counter =1;
return function(){
wb.height = window.innerHeight * (++counter);
window.scrollTo(0, window.innerHeight * (counter-1));
redraw();
}
}
newPage = createPage();