-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
96 lines (74 loc) · 2.26 KB
/
script.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
86
87
88
89
90
91
92
93
94
95
96
$(function() {
var vid = document.getElementById("vid"),
vid2 = document.getElementById("vid2");
var baseCanvas = document.getElementById("canvas"),
c = baseCanvas.getContext("2d"),
canvasWidth = baseCanvas.width,
canvasHeight = baseCanvas.height;
var merge = document.getElementById("merge"),
cMerge = merge.getContext("2d");
var outputCanvas = document.getElementById("output"),
cOutput = outputCanvas.getContext("2d");
var timeOut;
function brush(event) {
var $this = $(this);
$this.on({ // Attach multiple event handlers simultaneously using a plain object. (http://api.jquery.com/on/#on-events-selector-data)
mousemove: function(event) {
// get brush position
var xPos = event.pageX, // left
yPos = event.pageY; // top
var widthSpace = ($(window).width() - 640) / 2,
topSpace = $("#output").position().top;
var brushPosX = xPos - widthSpace,
brushPosY = yPos - topSpace;
// end of get brush position
c.fillStyle = "rgba(0, 0, 0, 1)";
c.beginPath();
c.arc(brushPosX, brushPosY, 27, 0, 2 * Math.PI, false);
c.fill();
},
mouseup: function() {
$this.off("mousemove");
}
});
}
function manip() {
cMerge.drawImage(vid, 0, 0, 640, 360);
cMerge.drawImage(baseCanvas, 0, 0, 640, 360);
var image = cMerge.getImageData(0, 0, canvasWidth, canvasHeight),
imageData = image.data,
length = imageData.length;
for ( var i = 0; i < length; i += 4 ) {
var r = imageData[i],
g = imageData[i+1],
b = imageData[i+2];
if (r == 0 && g == 0 && b == 0) {
imageData[i+3] = 0;
}
}
image.data = imageData;
cOutput.putImageData(image, 0, 0, 0, 0, canvasWidth, canvasHeight);
}
function startToLoop() {
if (vid.paused || vid.ended) {
return;
}
manip();
if (requestAnimationFrame) { // "requestAnimationFrame" https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame
requestAnimationFrame(startToLoop);
} else {
timeOut = setTimeout(startToLoop, 1000/60);
}
}
$(window).on("mousedown", function() {
vid2.play();
vid.play();
startToLoop();
brush();
});
vid.play();
startToLoop();
vid.addEventListener("ended", function() {
clearTimeout(timeOut);
});
});