blob: 064fdd535c7ee013057d2128308b11361d84fcdb [file] [log] [blame]
Cary Clarkbef063a2017-10-31 15:44:45 -04001
2var canvas
3var context
4var getFromWeb = true
5var mouseDown = false
6var scale = .6
7var outset = 15
8var columnWidth = 256
9var maxHeight = 256
10var lastLink = null
11var lastLinkStr = null
12var labelback = {}
13var loadedImages = 0
14var images = {}
15var imagesLength = 0;
16
17function recContains(rec, value) {
Cary Clarkd49980c2017-11-01 10:21:45 -040018 if (!value.length)
19 return 0
20 var lc = value.toLowerCase()
21 if (rec.name.toLowerCase().indexOf(lc) >= 0)
22 return 1
23 if (rec.code.toLowerCase().indexOf(lc) >= 0)
24 return 2
25 return 3
Cary Clarkbef063a2017-10-31 15:44:45 -040026}
27
28function setLink(recstr) {
29 var under
30 var link = recstr
Cary Clarkd49980c2017-11-01 10:21:45 -040031 if (!link.startsWith("Sk")) {
Cary Clarkbef063a2017-10-31 15:44:45 -040032 under = link.indexOf('_')
Cary Clarkd49980c2017-11-01 10:21:45 -040033 link = link.substring(under + 1)
Cary Clarkbef063a2017-10-31 15:44:45 -040034 }
35 under = link.lastIndexOf('_')
36 var len = link.length
Cary Clarkd49980c2017-11-01 10:21:45 -040037 if (under == len - 2) {
Cary Clarkbef063a2017-10-31 15:44:45 -040038 var letter = link[len - 1]
Cary Clarkd49980c2017-11-01 10:21:45 -040039 if ('a' <= letter && letter <= 'z') {
40 link = link.substr(0, len - 2)
41 } else if ('0' <= letter && letter <= '9') {
42 link = link.substr(0, len - 2)
43 }
Cary Clarkbef063a2017-10-31 15:44:45 -040044 }
Cary Clarkd49980c2017-11-01 10:21:45 -040045 lastLinkStr = link
Cary Clarkbef063a2017-10-31 15:44:45 -040046}
47
Cary Clarkd49980c2017-11-01 10:21:45 -040048function showLink() {
49 var link = lastLink.file + '#' + lastLinkStr
50 context.save()
Cary Clarkbef063a2017-10-31 15:44:45 -040051 context.font = "normal 16px Arial";
Cary Clarkd49980c2017-11-01 10:21:45 -040052 labelback.w = Math.max(labelback.w, context.measureText(link).width + 8)
53 context.beginPath()
54 context.rect(labelback.x, labelback.y, labelback.w, labelback.h)
55 context.fillStyle = "rgba(232,180,220, 1)"
56 context.fill()
57 context.fillStyle = "rgba(64,32,48, 1)"
58 context.fillText(link, labelback.x + 4, labelback.y + 16)
59 context.restore()
Cary Clarkbef063a2017-10-31 15:44:45 -040060}
61
Cary Clarkd49980c2017-11-01 10:21:45 -040062function imageIterator(callout, state) {
Cary Clarkbef063a2017-10-31 15:44:45 -040063 var row = outset + 30
64 var column = outset
65 for (var recstr in pngs) {
66 var rec = pngs[recstr]
67 var contains = recContains(rec, input.value)
Cary Clarkd49980c2017-11-01 10:21:45 -040068 if (3 == contains)
69 continue;
Cary Clarkbef063a2017-10-31 15:44:45 -040070 var height = rec.height < maxHeight ? rec.height : maxHeight
71 if (callout(state, column, row, height, contains, recstr))
72 break;
73 row += height + outset
74 if (row >= canvas.height / scale) {
75 row = 0
76 column += columnWidth + outset
77 if (column >= canvas.width / scale) {
78 break
79 }
80 }
81 }
82}
83
84function handleMouseOver(event) {
85 var callout = function(state, column, row, height, contains, recstr) {
86 if (state.x >= column && state.x <= column + columnWidth &&
87 state.y >= row && state.y <= row + height) {
88 document.body.style.cursor = "pointer"
89 lastLink = pngs[recstr]
90 setLink(recstr)
91 showLink()
92 return true
93 }
94 return false
95 }
96 var state = {
97 x: (event.clientX - 5) / scale,
98 y: (event.clientY - 7) / scale
99 }
100 document.body.style.cursor = ""
101 lastLink = null
102 imageIterator(callout, state)
103}
104
105function handleMouseClick() {
106 if (null != lastLink) {
107 var link = 'https://skia.org/user/api/' + lastLink.file + '#' + lastLinkStr
108 window.location = link
109 }
110}
111
Cary Clarkd49980c2017-11-01 10:21:45 -0400112function doKeyPress(evt) {
Cary Clarkbef063a2017-10-31 15:44:45 -0400113 idiv.style.height = 20
114 input.focus()
115}
116
Cary Clarkd49980c2017-11-01 10:21:45 -0400117function drawImage(hash, x, y, w, h, contains) {
Cary Clarkbef063a2017-10-31 15:44:45 -0400118 context.save()
119 context.transform(scale, 0, 0, scale, 0, 0)
120 context.save()
121 context.beginPath()
122 context.rect(x, y, w, h)
123 context.clip()
124 context.drawImage(images[hash], x, y)
125 context.restore()
126 context.beginPath()
127 context.rect(x, y, w, h)
128 context.strokeStyle = 1 == contains ? "red" : "black"
129 context.stroke()
130 context.restore()
131}
132
133function draw() {
134 var callout = function(state, column, row, height, contains, recstr) {
135 drawImage(pngs[recstr].hash, column, row, columnWidth, height, contains)
Cary Clarkd49980c2017-11-01 10:21:45 -0400136 return false
Cary Clarkbef063a2017-10-31 15:44:45 -0400137 }
138 imageIterator(callout, null)
139}
140
Cary Clarkd49980c2017-11-01 10:21:45 -0400141function sleep(ms) {
142 return new Promise(resolve => setTimeout(resolve, ms));
143}
144
145async function redraw() {
146 context.strokeStyle = "white"
Cary Clarkbef063a2017-10-31 15:44:45 -0400147 context.beginPath()
148 context.fillStyle = "white"
149 context.rect(0, 30, canvas.width, canvas.height)
150 context.fill()
151 context.rect((256 + outset) * scale, 0, canvas.width, 30)
Cary Clarkd49980c2017-11-01 10:21:45 -0400152 context.fill()
153 for (var image in images) {
154 image.drawn = false
155 }
156 do {
157 draw();
158 if (loadedImages >= imagesLength)
159 break;
160 console.debug(" loadedImages:" + loadedImages + " imagesLength:" + imagesLength)
161 await sleep(1000);
162 } while (true)
Cary Clarkbef063a2017-10-31 15:44:45 -0400163}
164
165function resize() {
166 setSize()
167 redraw()
168}
169
170function setSize() {
171 canvas.width = window.innerWidth - 20
172 canvas.height = window.innerHeight - 20
173 labelback.x = 0
174 labelback.y = canvas.height - 20
175 labelback.w = 0
176 labelback.h = 20
177}
178
Cary Clarkd49980c2017-11-01 10:21:45 -0400179function loadImages() {
180 for (var recstr in pngs) {
Cary Clarkbef063a2017-10-31 15:44:45 -0400181 var rec = pngs[recstr]
182 var image = new Image()
Cary Clarkd49980c2017-11-01 10:21:45 -0400183 images[rec.hash] = image
Cary Clarkbef063a2017-10-31 15:44:45 -0400184 if (getFromWeb)
185 image.src = 'https://fiddle.skia.org/i/'
186 image.src += rec.hash + '_raster.png'
Cary Clarkd49980c2017-11-01 10:21:45 -0400187 image.onload = function () {
188 loadedImages += 1
189 }
190 imagesLength += 1;
191 }
Cary Clarkbef063a2017-10-31 15:44:45 -0400192}
193
Cary Clarkd49980c2017-11-01 10:21:45 -0400194function start() {
Cary Clarkbef063a2017-10-31 15:44:45 -0400195 loadImages()
196 window.addEventListener('keypress', doKeyPress, true);
197 window.addEventListener('keydown', doKeyPress, true);
198 canvas = document.getElementById('canvas')
199 context = canvas.getContext('2d')
200 resize()
201}
202
203</script>
204</head>
205
206<body onLoad="start()" onresize="resize()">
Cary Clarkd49980c2017-11-01 10:21:45 -0400207<div style="height:0" id="idiv">
208<input type="text" id="input" onkeypress="redraw()" onkeydown="redraw()"/>
Cary Clarkbef063a2017-10-31 15:44:45 -0400209</div>
210<canvas id="canvas" width="750" height="500"
211onmousedown="mouseDown = true"
212onmouseup="mouseDown = false"
213onmousemove="handleMouseOver(event)"
214onclick="handleMouseClick()"
215></canvas >
216</body>
217</html>