-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
135 lines (112 loc) · 3.73 KB
/
index.html
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
<html>
<head>
<title>Prepare ye the way of the fjord</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
var resetPaperTimeout = null;
$(function() {
var ctx = getCanvasContext();
if (!ctx) return;
ctx.fillStyle = "rgb(150,150,150)";
ctx.fillRect (0, 0, 500, 500);
ctx.fillStyle = "rgba(0, 0, 200, 0.5)";
ctx.fillRect (30, 30, 55, 50);
resetPaper(function() {
$('body').css('background', 'white');
});
var previousMousePosition = null;
$('#paper').mousemove(function(event) {
var newPos = {x: event.offsetX, y: event.offsetY};
eraseAt(newPos, previousMousePosition);
previousMousePosition = newPos;
})
$('#paper').bind('mouseover mouseout', function() {
previousMousePosition = null;
})
})
var lastEraseTime = null;
function eraseAt(from, to) {
if (!to) return;
if (from.x === to.x && from.y === to.y) return;
var lx = to.x - from.x;
var ly = to.y - from.y;
var lineLength = Math.sqrt(lx*lx + ly*ly);
if (lineLength > 50 && lastEraseTime && lastEraseTime - Date.now() > 500) return;
lastEraseTime = Date.now()
var lineWidth = 20;
var wy = lx / lineLength * lineWidth;
var wx = ly / lineLength * lineWidth;
var ctx = getCanvasContext();
var gradient = ctx.createLinearGradient(from.x-wx/2, from.y+wy/2, from.x+wx/2, from.y-wy/2);
gradient.addColorStop(0, "rgba(0,0,0, 0)");
gradient.addColorStop(0.43, "rgba(0,0,0, 0.8)");
gradient.addColorStop(0.57, "rgba(0,0,0, 0.8)");
gradient.addColorStop(1, "rgba(0,0,0, 0)");
ctx.save();
ctx.lineCap = "butt";
ctx.globalCompositeOperation = "destination-out";
ctx.beginPath();
ctx.lineWidth = lineWidth;
ctx.strokeStyle = gradient;
ctx.moveTo(from.x, from.y);
ctx.lineTo(to.x, to.y);
ctx.stroke();
ctx.restore();
}
var _cachedContext = undefined;
function getCanvasContext() {
if (typeof _cachedContext === 'undefined') {
var canvas = $('#paper');
canvas = canvas[0];
if (canvas.getContext) {
_cachedContext = canvas.getContext('2d');
} else {
_cachedContext = null;
}
}
return _cachedContext;
}
var _resettingPaper = false;
function resetPaper(callback) {
if (_resettingPaper) return;
$('body').css('background', 'white');
_resettingPaper = true;
$('#paper').attr('height', $(window).height()).attr('width', $(window).width());
var ctx = getCanvasContext();
if (!ctx) return;
var img = new Image(); // Create new img element
img.onload = function() {
var imgWidth = img.width;
var imgHeight = img.height;
var canvasHeight = $('#paper').height();
var canvasWidth = $('#paper').width();
for (var y = 0; y < canvasHeight; y += imgHeight) {
for (var x = 0; x < canvasWidth; x += imgWidth) {
var drawWidth = Math.min(imgWidth, canvasWidth - x);
var drawHeight = Math.min(imgHeight, canvasHeight - y);
ctx.drawImage(img, 0, 0, drawWidth, drawHeight, x, y, drawWidth, drawHeight);
}
}
_resettingPaper = false;
$('body').css('background', 'white');
if (typeof callback === 'function') return callback();
};
img.src = 'images/paper-texture.png'; // Set source path
}
$(window).resize(resetPaper)
</script>
<style type="text/css">
canvas {
}
body {
background: white;
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<canvas id="paper" width="1" height="1"></canvas>
</body>
</html>