-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
168 lines (139 loc) · 4.95 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>HTML DOM Patterns</title>
<style type="text/css">
.pattern {
border: 10px solid black;
overflow: hidden;
position: relative;
}
.pattern * {
position: absolute;
}
.patch {
background-color: SteelBlue;
overflow: hidden;
}
.patch .pixel {
background-color: DeepSkyBlue;
}
</style>
</head>
<body>
<script>
// str => String to use as a template when creating a the patch
// textSize => The pixel size of the text
// pixelSize => The "pixel accuracy" of how closely the template should be followed.
// A value of 3 means each outputted pixel is 3x3.
// A value of 1 means a pixel-perfect representation of the passed in 'str'
function createPatch(str, textSize, pixelSize){
// Text is taller than it is wide, so this won't actually give a valid
// result, it will just make the canvas wider than it needs to be. In
// order to not waste "canvas pixels" (because they are a rare commodity)
// we need to find a way to accurately measure the resulting with
// from whatever the HTML5 equivalent of a TextField is. If you can read this, you either have line wrapping on; if not "Haha! Made you scroll!" http://cdn-static.zdnet.com/i/story/60/05/003301/nelson-muntz.gif
var w = textSize * str.length;
var h = textSize + 3; // some sort of buffer for stuff under the baseline
var patch = document.createElement("div");
patch.className = "patch";
var canvas = document.createElement("canvas");
canvas.width = w;
canvas.height = h;
var ctx = canvas.getContext("2d");
ctx.fillStyle = "black";
ctx.font = textSize + "px monospace"; // "px Consolas"
ctx.fillText(str, 0, textSize, w);
var pixels = ctx.getImageData(0, 0, w, h).data;
var nextPixel = 4 /* bytes */; // "Distance to" next pixel, that is.
for(var i = 0; i < pixels.length; i += nextPixel){
var pixelAlpha = pixels[i + 3];
if(!pixelAlpha) continue;
var pixelIndex = Math.floor(i / 4);
var x = Math.floor(pixelIndex % w) * pixelSize;
var y = Math.floor(pixelIndex / w) * pixelSize;
var pixel = createElemAt("pixel", x, y, pixelSize);
pixel.style.opacity = pixelAlpha / 255;
patch.appendChild(pixel);
}
return patch;
}
function createElemAt(className, x, y, size){
var elem = document.createElement("div");
elem.className = className + " p" + x + "x" + y;
elem.style.left = x + "px";
elem.style.top = y + "px";
elem.style.width = size + "px";
elem.style.height = size + "px";
return elem;
}
function createPattern(countX, countY, width, height, srcPatch){
var pattern = document.createElement("div");
pattern.className = "pattern";
pattern.style.width = countX * width + "px";
pattern.style.height = countY * height + "px";
for (var y=0; y<countY; y++){
for (var x=0; x<countX; x++){
var patch = srcPatch.cloneNode(true);
patch.style.left = x * width + "px";
patch.style.top = y * height + "px";
patch.style.width = width + "px";
patch.style.height = height + "px";
pattern.appendChild(patch);
}
}
return pattern;
}
function attachBehavior(target){
document.addEventListener("click", handleRemoveClickedPixel);
document.addEventListener("click", handleAddPixelOnClick);
}
// removes pixels from all instances of the patches
// of a pattern when clicked
function handleRemoveClickedPixel(event){
var classes = event.target.className;
if (classes.indexOf("pixel") == -1) return;
var selector = "." + classes.split(" ").join(".");
var matches = document.querySelectorAll(selector);
for (var i=0, n=matches.length; i<n; i++){
var match = matches[i];
match.parentNode.removeChild(match);
}
}
// adds a pixel where a patch is clicked
function handleAddPixelOnClick(event){
// verify the current click target is a patch
if (event.target.className !== "patch"){
return;
}
var patch = event.target;
// base pixel size off of size of existing pixels
var existingPixel = document.querySelector(".patch .pixel");
var pixelSize = existingPixel.getBoundingClientRect().width;
// get the position of the new pixel based
// on the mouse position within the clicked patch
var patchRect = patch.getBoundingClientRect();
var x = event.clientX - patch.clientLeft - patchRect.left;
var y = event.clientY - patch.clientTop - patchRect.top;
// snap the position within the pixel grid (based on pixel size)
x = pixelSize * Math.floor(x/pixelSize);
y = pixelSize * Math.floor(y/pixelSize);
// create the pixel to add
var pixel = createElemAt("pixel", x, y, pixelSize);
// add the pixel to all patches
var patches = document.querySelectorAll(".patch");
for (var i=0, n=patches.length; i<n; i++){
var pixelClone = pixel.cloneNode(true);
patches[i].appendChild(pixelClone);
}
}
// The spaces are to pad the left a bit, since we have no way of doing that by
// code (yet).
var patch = createPatch("O_o", 8, 5);
var pattern = createPattern(10, 8, 100, 50, patch);
attachBehavior(pattern);
document.body.appendChild(pattern);
</script>
</body>
</html>