-
Notifications
You must be signed in to change notification settings - Fork 0
/
cursor.js
310 lines (287 loc) · 10.5 KB
/
cursor.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
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
(function (Scratch) {
'use strict';
if (!Scratch.extensions.unsandboxed) {
throw new Error('MouseCursor extension must be run unsandboxed');
}
const lazilyCreatedCanvas = () => {
/** @type {HTMLCanvasElement} */
let canvas = null;
/** @type {CanvasRenderingContext2D} */
let ctx = null;
/**
* @param {number} width
* @param {number} height
* @returns {[HTMLCanvasElement, CanvasRenderingContext2D]}
*/
return (width, height) => {
if (!canvas) {
canvas = document.createElement('canvas');
ctx = canvas.getContext('2d');
if (!ctx) {
throw new Error('Could not get 2d rendering context');
}
}
// Setting canvas size also clears it
canvas.width = width;
canvas.height = height;
return [canvas, ctx];
};
};
const getRawSkinCanvas = lazilyCreatedCanvas();
/**
* @param {RenderWebGL.Skin} skin
* @returns {string} A data: URI for the skin.
*/
const encodeSkinToURL = (skin) => {
const svgSkin = /** @type {RenderWebGL.SVGSkin} */ (skin);
if (svgSkin._svgImage) {
// This is an SVG skin
return svgSkin._svgImage.src;
}
// It's probably a bitmap skin.
// The most reliable way to get the bitmap in every runtime is through the silhouette.
// This is very slow and could involve reading the texture from the GPU.
const silhouette = skin._silhouette;
// unlazy() only exists in TW
// @ts-expect-error
if (silhouette.unlazy) {
// @ts-expect-error
silhouette.unlazy();
}
const colorData = silhouette._colorData;
const width = silhouette._width;
const height = silhouette._height;
const imageData = new ImageData(colorData, silhouette._width, silhouette._height);
const [canvas, ctx] = getRawSkinCanvas(width, height);
ctx.putImageData(imageData, 0, 0);
return canvas.toDataURL();
};
/**
* @param {VM.Costume} costume
* @param {number} maxWidth
* @param {number} maxHeight
* @returns {{uri: string, width: number, height: number}}
*/
const costumeToCursor = (costume, maxWidth, maxHeight) => {
const skin = Scratch.vm.renderer._allSkins[costume.skinId];
const imageURI = encodeSkinToURL(skin);
let width = skin.size[0];
let height = skin.size[1];
if (width > maxWidth) {
height = height * (maxWidth / width);
width = maxWidth;
}
if (height > maxHeight) {
width = width * (maxHeight / height);
height = maxHeight;
}
width = Math.round(width);
height = Math.round(height);
// We wrap the encoded image in an <svg>. This lets us do some clever things:
// - We can resize the image without a canvas.
// - We can give the browser an image with more raw pixels than its DPI independent size
// The latter is important so that cursors won't look horrible on high DPI displays. For
// example, if the cursor will display at 32x32 in DPI independent units on a 2x high DPI
// display, we actually need to send a 64x64 image for it to look good. This lets us do
// that automatically.
let svg = `<svg xmlns="http://www.w3.org/2000/svg" width="${width}" height="${height}">`;
svg += `<image href="${imageURI}" width="${width}" height="${height}" />`;
svg += '</svg>';
// URI encoding usually results in smaller string than base 64 for the types of data we get here.
const svgURI = `data:image/svg+xml;,${encodeURIComponent(svg)}`;
return {
uri: svgURI,
width,
height
};
};
/** @type {string} */
let nativeCursor = 'default';
/** @type {null|string} */
let customCursorImageName = null;
const canvas = Scratch.renderer.canvas;
/** @type {string} */
let currentCanvasCursor = nativeCursor;
const updateCanvasCursor = () => {
if (canvas.style.cursor !== currentCanvasCursor) {
canvas.style.cursor = currentCanvasCursor;
}
};
// scratch-gui will sometimes reset the cursor when resizing the window or going in/out of fullscreen
new MutationObserver(updateCanvasCursor).observe(canvas, {
attributeFilter: ['style'],
attributes: true
});
/**
* Parse strings like "60x12" or "77,1"
* @param {string} string
* @returns {[number, number]}
*/
const parseTuple = (string) => {
const [a, b] = ('' + string).split(/[ ,x]/);
return [
+a || 0,
+b || 0
];
};
class MouseCursor {
getInfo() {
return {
id: 'MouseCursor',
name: 'Mouse Cursor',
blocks: [
{
opcode: 'setCur',
blockType: Scratch.BlockType.COMMAND,
text: 'set cursor to [cur]',
arguments: {
cur: {
type: Scratch.ArgumentType.STRING,
defaultValue: 'pointer',
menu: 'cursors',
},
},
},
{
opcode: 'setCursorImage',
blockType: Scratch.BlockType.COMMAND,
text: "set cursor to current costume center: [position] max size: [size]",
arguments: {
position: {
type: Scratch.ArgumentType.STRING,
defaultValue: '0,0',
menu: 'imagePositions'
},
size: {
type: Scratch.ArgumentType.STRING,
defaultValue: '32x32',
menu: 'imageSizes'
}
}
},
{
opcode: 'hideCur',
blockType: Scratch.BlockType.COMMAND,
text: 'hide cursor',
},
{
opcode: 'getCur',
blockType: Scratch.BlockType.REPORTER,
text: 'cursor',
},
],
menus: {
cursors: {
acceptReporters: true,
items: [
{ text: 'default', value: 'default' },
{ text: 'pointer', value: 'pointer' },
{ text: 'move', value: 'move' },
{ text: 'grab', value: 'grab' },
{ text: 'grabbing', value: 'grabbing' },
{ text: 'text', value: 'text' },
{ text: 'vertical-text', value: 'vertical-text' },
{ text: 'wait', value: 'wait' },
{ text: 'progress', value: 'progress' },
{ text: 'help', value: 'help' },
{ text: 'context-menu', value: 'context-menu' },
{ text: 'zoom-in', value: 'zoom-in' },
{ text: 'zoom-out', value: 'zoom-out' },
{ text: 'crosshair', value: 'crosshair' },
{ text: 'cell', value: 'cell' },
{ text: 'not-allowed', value: 'not-allowed' },
{ text: 'copy', value: 'copy' },
{ text: 'alias', value: 'alias' },
{ text: 'no-drop', value: 'no-drop' },
{ text: 'all-scroll', value: 'all-scroll' },
{ text: 'col-resize', value: 'col-resize' },
{ text: 'row-resize', value: 'row-resize' },
{ text: 'n-resize', value: 'n-resize' },
{ text: 'e-resize', value: 'e-resize' },
{ text: 's-resize', value: 's-resize' },
{ text: 'w-resize', value: 'w-resize' },
{ text: 'ne-resize', value: 'ne-resize' },
{ text: 'nw-resize', value: 'nw-resize' },
{ text: 'se-resize', value: 'se-resize' },
{ text: 'sw-resize', value: 'sw-resize' },
{ text: 'ew-resize', value: 'ew-resize' },
{ text: 'ns-resize', value: 'ns-resize' },
{ text: 'nesw-resize', value: 'nesw-resize' },
{ text: 'nwse-resize', value: 'nwse-resize' },
],
},
imagePositions: {
acceptReporters: true,
items: [
// [x, y] where x is [0=left, 100=right] and y is [0=top, 100=bottom]
{ text: 'top left', value: '0,0' },
{ text: 'top right', value: '100,0' },
{ text: 'bottom left', value: '0,100' },
{ text: 'bottom right', value: '100,100' },
{ text: 'center', value: '50,50' },
]
},
imageSizes: {
acceptReporters: true,
items: [
// Some important numbers to keep in mind:
// Browsers ignore cursor images >128 in any dimension (https://searchfox.org/mozilla-central/rev/43ee5e789b079e94837a21336e9ce2420658fd19/widget/gtk/nsWindow.cpp#3393-3402)
// Browsers may refuse to display a cursor near window borders for images >32 in any dimension
{ text: '4x4', value: '4x4' },
{ text: '8x8', value: '8x4' },
{ text: '12x12', value: '12x12' },
{ text: '16x16', value: '16x16' },
{ text: '32x32', value: '32x32' },
{ text: '48x48 (unreliable)', value: '48x48' },
{ text: '64x64 (unreliable)', value: '64x64' },
{ text: '128x128 (unreliable)', value: '128x128' },
]
}
},
};
}
setCur(args) {
const newCursor = args.cur;
nativeCursor = newCursor;
customCursorImageName = null;
currentCanvasCursor = newCursor;
updateCanvasCursor();
}
setCursorImage(args, util) {
const [maxWidth, maxHeight] = parseTuple(args.size).map(i => Math.max(0, i));
const currentCostume = util.target.getCostumes()[util.target.currentCostume];
const costumeName = currentCostume.name;
let encodedCostume;
try {
encodedCostume = costumeToCursor(currentCostume, maxWidth, maxHeight);
} catch (e) {
// This could happen for a variety of reasons.
console.error(e);
}
if (encodedCostume) {
const [percentX, percentY] = parseTuple(args.position).map(i => Math.max(0, Math.min(100, i)) / 100);
const x = percentX * encodedCostume.width;
const y = percentY * encodedCostume.height;
currentCanvasCursor = `url("${encodedCostume.uri}") ${x} ${y}, ${nativeCursor}`;
updateCanvasCursor();
} else {
// If for some reason the costume couldn't be encoded, we'll leave the cursor unchanged.
// This is the same behavior that would happen if we successfully encode a cursor but the browser
// is unable to parse it for some reason.
}
customCursorImageName = costumeName;
}
hideCur() {
this.setCur({
cur: 'none'
});
}
getCur() {
if (customCursorImageName !== null) {
return customCursorImageName;
}
return nativeCursor;
}
}
Scratch.extensions.register(new MouseCursor());
})(Scratch);