-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.html
More file actions
251 lines (206 loc) · 7.35 KB
/
index.html
File metadata and controls
251 lines (206 loc) · 7.35 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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<html>
<head>
<script src="https://cdn.firebase.com/js/client/1.0.15/firebase.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.11.1/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/ui/1.11.1/jquery-ui.js"></script>
<link rel="stylesheet" type="text/css" href="example.css">
<link href="style.css" rel="stylesheet" type="text/css" media="all">
</head>
<body>
<div class="l-demo-container">
<canvas id="drawing-canvas" width="1080" height="620"></canvas>
</div>
<div id="colorholder"></div>
<script>
$(document).ready(function () {
event.preventDefault();
//Set up some globals
var lastPoint = null;
currentColor = "000";
mouseDown = 0;
xy = "";
brushStyle = "solid";
pixSize = 10;
$(function() {
$( "#slider" ).slider({
value: pixSize,
min:1,
max:50,
orientation: "horizontal",
range: "min",
animate: true,
slide: function( event, ui ) {
pixSize = $( "#slider" ).slider( "value" );
$('#brushWidth').val(pixSize);
}
});
});
//Create a reference to the pixel data for our drawing.
var pixelDataRef = new Firebase('https://draw-with-me.firebaseio.com/');
// Set up our canvas
var myCanvas = document.getElementById('drawing-canvas');
var myContext = myCanvas.getContext ? myCanvas.getContext('2d') : null;
if (myContext == null) {
alert("You must use a browser that supports HTML5 Canvas to run this demo.");
return;
}
//Setup each color palette & add it to the screen
var colors = ["fff","000","f00","0f0","00f","88f","f8d","f88","f05","f80","0f8","cf0","08f","408","ff8","8ff"];
for (c in colors) {
var item = $('<div/>').css("background-color", '#' + colors[c]).addClass("colorbox");
item.click((function () {
var col = colors[c];
return function () {
currentColor = col;
};
})());
item.appendTo('#colorholder');
}
//Keep track of if the mouse is up or down
myCanvas.onmousedown = function () {mouseDown = 1;};
myCanvas.onmouseout = myCanvas.onmouseup = function () {
mouseDown = 0; lastPoint = null;
pixelDataRef.child('priorityCounter').set(priorityCounter);
/* pixelDataRef.child(cur_line_num).set({color:currentColor, pixSize:pixSize, brushStyle:brushStyle}); */
lineVisible++;
pixelDataRef.child('lineVisible').set(lineVisible);
cur_line_num++;
pixelDataRef.child('cur_line_num').set(cur_line_num);
};
pixelDataRef.child('priorityCounter').once('value', function(snapshot) {
if (snapshot.val() === null) {
priorityCounter = 1;
pixelDataRef.child('priorityCounter').set(priorityCounter);
} else {
priorityCounter = snapshot.val();
console.log(priorityCounter);
}
});
pixelDataRef.child('cur_line_num').once('value', function(snapshot) {
if (snapshot.val() === null) {
cur_line_num = 1;
pixelDataRef.child('cur_line_num').set(cur_line_num);
} else {
cur_line_num = snapshot.val();
console.log(cur_line_num);
}
});
pixelDataRef.child('lineVisible').once('value', function(snapshot) {
if (snapshot.val() === null) {
lineVisible = 1;
pixelDataRef.child('lineVisible').set(lineVisible);
} else {
lineVisible = snapshot.val();
console.log(lineVisible);
}
});
/* console.log("a"+ pixSize); */
function getWidthSlider() {
pixSize = $( "#slider" ).slider( "option", "value" );
return pixSize;
};
//Draw a line from the mouse's last position to its current position
var drawLineOnMouseMove = function(e) {
if (!mouseDown) return;
e.preventDefault();
// Bresenham's line algorithm. We use this to ensure smooth lines are drawn
var offset = $('canvas').offset();
var x1 = Math.floor((e.pageX - offset.left)),
y1 = Math.floor((e.pageY - offset.top));
var x0 = (lastPoint == null) ? x1 : lastPoint[0];
var y0 = (lastPoint == null) ? y1 : lastPoint[1];
var dx = Math.abs(x1 - x0), dy = Math.abs(y1 - y0);
var sx = (x0 < x1) ? 1 : -1, sy = (y0 < y1) ? 1 : -1, err = dx - dy;
while (true) {
//write the pixel into Fi=currebase, or if we are drawing white, remove the pixel
xy = x0 + ":" + y0 + ":" + cur_line_num;
xy = xy.toString();
pixSize = getWidthSlider();
var info_dict = {currentColor:currentColor, pixSize:pixSize, brushStyle:brushStyle};
/*
console.log("a"+ currentColor);
console.log("a"+ pixSize);
console.log("a"+ brushStyle);
*/
pixelDataRef.child(xy).setWithPriority(info_dict, priorityCounter);
priorityCounter++;
if (x0 == x1 && y0 == y1) break;
var e2 = 2 * err;
if (e2 > -dy) {
err = err - dy;
x0 = x0 + sx;
}
if (e2 < dx) {
err = err + dx;
y0 = y0 + sy;
}
}
lastPoint = [x1, y1];
};
$(myCanvas).mousemove(drawLineOnMouseMove);
$(myCanvas).mousedown(drawLineOnMouseMove);
// Add callbacks that are fired any time the pixel data changes and adjusts the canvas appropriately.
// Note that child_added events will be fired for initial pixel data as well.
var drawPixel = function(snapshot) {
var coords = snapshot.name().split(":");
if (!isNaN(coords[0])) {
line_number = parseInt(coords[2]);
if (line_number <= lineVisible) {
/* temp1 = "#" + snapshot.val().currentColor; */
myContext.fillStyle = "#" + snapshot.val().currentColor;
pixSize = snapshot.val().pixSize;
brushStyle = snapshot.val().brushStyle;
x00 = parseInt(coords[0]);
y00 = parseInt(coords[1]);
myContext.fillRect(x00, y00, pixSize, pixSize);
}
}
};
$('#undo').on('click', function() {
//line_number = style:display:none
//line_number-- = or into array?
lineVisible--;
console.log(lineVisible+"-");
pixelDataRef.child('lineVisible').set(lineVisible);
//redraw whole canvas
drawPixel;
});
$('#redo').on('click', function() {
lineVisible++;
console.log(lineVisible+"+");
pixelDataRef.child('lineVisible').set(lineVisible);
//redraw whole canvas
drawPixel;
});
var clearPixel = function(snapshot) {
console.log("shouldn't be here");
var coords = snapshot.name().split(":");
if (isNaN(snapshot.val()[1])) {
pixSize = 1;
} else {
pixSize = snapshot.val()[1];
}
if (pixSize > 1) {
for (var i = 0; i < pixSize; i++) {
for (var j = 0; j < pixSize; j++) {
x0 += i;
y0 += j;
myContext.fillRect(parseInt(coords[0]), parseInt(coords[1]), 1, 1);
}
}
}
myContext.clearRect(parseInt(coords[0]), parseInt(coords[1]), pixSize, pixSize);
};
pixelDataRef.on('child_added', drawPixel);
pixelDataRef.on('child_changed', drawPixel);
pixelDataRef.on('child_removed', clearPixel);
});
</script>
<div id="slider" style="width:250px; margin:15px;"><br>
Brush Width: <input type="text" id="brushWidth" value="1" readonly style="border:0; color:#341f07; font-weight:bold;">
</div>
<button id="undo">Undo</button>
<button id="redo">Redo</button>
</body>
</html>