frigame3 0.0.0-stage → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +13 -0
- package/LICENSE +373 -0
- package/README.md +80 -2
- package/biome.json +93 -0
- package/demo/package-lock.json +515 -0
- package/demo/package.json +19 -0
- package/demo/snake/index.html +16 -0
- package/demo/snake/index.ts +110 -0
- package/demo/tetris/index.html +16 -0
- package/demo/tetris/index.ts +536 -0
- package/demo/tsconfig.json +53 -0
- package/frigame.css +12 -0
- package/package.json +50 -4
- package/src/Animation.ts +266 -0
- package/src/BaseSprite.ts +682 -0
- package/src/Gradient.ts +120 -0
- package/src/Playground.ts +247 -0
- package/src/Rect.ts +237 -0
- package/src/Rectangle.ts +108 -0
- package/src/Renderer.ts +28 -0
- package/src/ResourceManager.ts +145 -0
- package/src/Sprite.ts +484 -0
- package/src/SpriteGroup.ts +258 -0
- package/src/canvasRenderer/renderGroup.ts +194 -0
- package/src/canvasRenderer/renderPlayground.ts +103 -0
- package/src/canvasRenderer/renderRectangle.ts +421 -0
- package/src/canvasRenderer/renderSprite.ts +148 -0
- package/src/canvasRenderer.ts +38 -0
- package/src/dummyRenderer.ts +62 -0
- package/src/plugins/GamepadTracker.ts +97 -0
- package/src/plugins/Tilemap.ts +123 -0
- package/src/plugins/Vec2.ts +420 -0
- package/src/plugins/fx/Shaker.ts +293 -0
- package/src/plugins/fx/Tweener.ts +252 -0
- package/src/plugins/fx/easing.ts +276 -0
- package/src/plugins/isometric/ISOMixin.ts +444 -0
- package/src/plugins/isometric/ISORectangle.ts +183 -0
- package/src/plugins/isometric/ISOSprite.ts +228 -0
- package/src/plugins/isometric/ISOSpriteGroup.ts +155 -0
- package/src/plugins/isometric/ISOTilemap.ts +122 -0
- package/src/plugins/isometric/utils.ts +19 -0
- package/src/plugins/keyTracker.ts +31 -0
- package/src/plugins/mixer.ts +523 -0
- package/src/plugins/sorted/SortedAnimation.ts +53 -0
- package/src/plugins/sorted/SortedGroup.ts +94 -0
- package/src/plugins/sorted/SortedMixin.ts +203 -0
- package/src/plugins/sorted/SortedRectangle.ts +35 -0
- package/src/plugins/sorted/SortedSprite.ts +56 -0
- package/src/utils.ts +53 -0
- package/test/animation/index.html +20 -0
- package/test/animation/index.ts +253 -0
- package/test/animation/mh.png +0 -0
- package/test/animation/moh.png +0 -0
- package/test/animation/mov.png +0 -0
- package/test/animation/mv.png +0 -0
- package/test/animation/rebound.png +0 -0
- package/test/animation/reboundm.png +0 -0
- package/test/animation/sh.png +0 -0
- package/test/animation/soh.png +0 -0
- package/test/animation/sov.png +0 -0
- package/test/animation/sv.png +0 -0
- package/test/isometric1/beach.png +0 -0
- package/test/isometric1/foglie.png +0 -0
- package/test/isometric1/grass.png +0 -0
- package/test/isometric1/index.html +17 -0
- package/test/isometric1/index.ts +68 -0
- package/test/isometric1/tronco.png +0 -0
- package/test/isometric1/untitled.tmx +25 -0
- package/test/isometric1/water.png +0 -0
- package/test/isometric2/banner.png +0 -0
- package/test/isometric2/books.png +0 -0
- package/test/isometric2/candlestick.png +0 -0
- package/test/isometric2/ice.png +0 -0
- package/test/isometric2/index.html +16 -0
- package/test/isometric2/index.ts +129 -0
- package/test/isometric2/knight_se.png +0 -0
- package/test/isometric2/tile.png +0 -0
- package/test/isometric2/untitled.tmx +36 -0
- package/test/isometric2/wall.png +0 -0
- package/test/layers/boy.png +0 -0
- package/test/layers/catgirl.png +0 -0
- package/test/layers/fri1.svg +8 -0
- package/test/layers/grassblock.png +0 -0
- package/test/layers/horngirl.png +0 -0
- package/test/layers/index.html +17 -0
- package/test/layers/index.ts +191 -0
- package/test/layers/pinkgirl.png +0 -0
- package/test/layers/render_ruler.py +57 -0
- package/test/layers/ruler.png +0 -0
- package/test/layers/sh.png +0 -0
- package/test/mixer/dieci.opus +0 -0
- package/test/mixer/dieci.wav +0 -0
- package/test/mixer/index.html +33 -0
- package/test/mixer/index.ts +206 -0
- package/test/mixer/news_intro.mp3 +0 -0
- package/test/mixer/news_intro.ogg +0 -0
- package/test/mixer/prova.mp3 +0 -0
- package/test/mixer/prova.ogg +0 -0
- package/test/package-lock.json +515 -0
- package/test/package.json +19 -0
- package/test/sorted/Brown Block.png +0 -0
- package/test/sorted/index.html +16 -0
- package/test/sorted/index.ts +38 -0
- package/test/transformation1/index.html +17 -0
- package/test/transformation1/index.ts +51 -0
- package/test/transformation1/sh.png +0 -0
- package/test/transformation2/index.html +20 -0
- package/test/transformation2/index.ts +71 -0
- package/test/transformation2/sh.png +0 -0
- package/test/tsconfig.json +53 -0
- package/tsconfig.json +51 -0
|
@@ -0,0 +1,421 @@
|
|
|
1
|
+
import type { GradientType } from "../Gradient.js";
|
|
2
|
+
import type { Rectangle } from "../Rectangle.js";
|
|
3
|
+
import { playgroundMap } from "./renderPlayground.js";
|
|
4
|
+
|
|
5
|
+
export interface GradientObj {
|
|
6
|
+
width: number;
|
|
7
|
+
height: number;
|
|
8
|
+
startColorStr: string;
|
|
9
|
+
endColorStr: string;
|
|
10
|
+
type: GradientType;
|
|
11
|
+
gradient: CanvasGradient | null;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export const fillMap: WeakMap<Rectangle, GradientObj> = new WeakMap<
|
|
15
|
+
Rectangle,
|
|
16
|
+
GradientObj
|
|
17
|
+
>();
|
|
18
|
+
|
|
19
|
+
export const strokeMap: WeakMap<Rectangle, GradientObj> = new WeakMap<
|
|
20
|
+
Rectangle,
|
|
21
|
+
GradientObj
|
|
22
|
+
>();
|
|
23
|
+
|
|
24
|
+
export function initRectangle(rectangle: Rectangle): void {
|
|
25
|
+
for (const map of [fillMap, strokeMap]) {
|
|
26
|
+
map.set(rectangle, {
|
|
27
|
+
width: 0,
|
|
28
|
+
height: 0,
|
|
29
|
+
startColorStr: "rgba(0,0,0,1)",
|
|
30
|
+
endColorStr: "rgba(0,0,0,1)",
|
|
31
|
+
type: "vertical",
|
|
32
|
+
gradient: null,
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function removeRectangle(_rectangle: Rectangle): void {
|
|
38
|
+
// no-op
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function drawRectangle(rectangle: Rectangle, _interp: number): void {
|
|
42
|
+
const playground = rectangle.playground!;
|
|
43
|
+
|
|
44
|
+
const trunc = Math.trunc;
|
|
45
|
+
const left = rectangle._drawLeft;
|
|
46
|
+
const top = rectangle._drawTop;
|
|
47
|
+
const width = rectangle._drawWidth;
|
|
48
|
+
const height = rectangle._drawHeight;
|
|
49
|
+
|
|
50
|
+
const insidePlayground = playground._insidePlayground(
|
|
51
|
+
left,
|
|
52
|
+
top,
|
|
53
|
+
width,
|
|
54
|
+
height,
|
|
55
|
+
);
|
|
56
|
+
const background = rectangle._background;
|
|
57
|
+
const border_width = trunc(rectangle._borderWidth);
|
|
58
|
+
const border_color = rectangle._borderColor;
|
|
59
|
+
const opacity = rectangle._opacity;
|
|
60
|
+
const scaleh = rectangle._scaleh;
|
|
61
|
+
const scalev = rectangle._scalev;
|
|
62
|
+
|
|
63
|
+
if (
|
|
64
|
+
insidePlayground &&
|
|
65
|
+
(background !== null || (border_width > 0 && border_color !== null)) &&
|
|
66
|
+
opacity !== 0 &&
|
|
67
|
+
scaleh !== 0 &&
|
|
68
|
+
scalev !== 0 &&
|
|
69
|
+
!rectangle._hidden
|
|
70
|
+
) {
|
|
71
|
+
const playgroundData = playgroundMap.get(playground)!;
|
|
72
|
+
const ctx = playgroundData.ctx;
|
|
73
|
+
|
|
74
|
+
let old_alpha = 1;
|
|
75
|
+
let alpha_changed = false;
|
|
76
|
+
if (opacity !== 1) {
|
|
77
|
+
// Don't save the entire context only for opacity changes
|
|
78
|
+
old_alpha = ctx.globalAlpha;
|
|
79
|
+
|
|
80
|
+
ctx.globalAlpha = old_alpha * opacity;
|
|
81
|
+
|
|
82
|
+
alpha_changed = true;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
let old_blend_mode: GlobalCompositeOperation = "source-over";
|
|
86
|
+
let blend_mode_changed = false;
|
|
87
|
+
const blendMode = rectangle._blendMode;
|
|
88
|
+
if (blendMode !== "normal") {
|
|
89
|
+
old_blend_mode = ctx.globalCompositeOperation;
|
|
90
|
+
|
|
91
|
+
if (blendMode === "add") {
|
|
92
|
+
ctx.globalCompositeOperation = "lighter";
|
|
93
|
+
} else {
|
|
94
|
+
ctx.globalCompositeOperation = blendMode;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
blend_mode_changed = true;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const angle = rectangle._angle;
|
|
101
|
+
const border_radius = trunc(rectangle._borderRadius);
|
|
102
|
+
const border_half_width = border_width / 2;
|
|
103
|
+
|
|
104
|
+
if (angle !== 0 || scaleh !== 1 || scalev !== 1) {
|
|
105
|
+
ctx.save();
|
|
106
|
+
|
|
107
|
+
let transformOriginx = rectangle._transformOriginx;
|
|
108
|
+
|
|
109
|
+
if (typeof transformOriginx === "string") {
|
|
110
|
+
transformOriginx = rectangle[transformOriginx];
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
transformOriginx = trunc(transformOriginx);
|
|
114
|
+
|
|
115
|
+
let transformOriginy = rectangle._transformOriginy;
|
|
116
|
+
|
|
117
|
+
if (typeof transformOriginy === "string") {
|
|
118
|
+
transformOriginy = rectangle[transformOriginy];
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
transformOriginy = trunc(transformOriginy);
|
|
122
|
+
|
|
123
|
+
ctx.translate(left + transformOriginx, top + transformOriginy);
|
|
124
|
+
|
|
125
|
+
if (angle !== 0) {
|
|
126
|
+
ctx.rotate(angle);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
if (scaleh !== 1 || scalev !== 1) {
|
|
130
|
+
ctx.scale(scaleh, scalev);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
ctx.translate(-transformOriginx, -transformOriginy);
|
|
134
|
+
|
|
135
|
+
if (background !== null) {
|
|
136
|
+
// Prepare a rect path for the background and the clipping region
|
|
137
|
+
ctx.beginPath();
|
|
138
|
+
|
|
139
|
+
if (border_radius > 0) {
|
|
140
|
+
ctx.roundRect(0, 0, width, height, border_radius);
|
|
141
|
+
} else {
|
|
142
|
+
ctx.rect(0, 0, width, height);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
ctx.closePath();
|
|
146
|
+
|
|
147
|
+
setFillStyle(ctx, rectangle);
|
|
148
|
+
ctx.fill();
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
if (border_width > 0 && border_color !== null) {
|
|
152
|
+
ctx.beginPath();
|
|
153
|
+
|
|
154
|
+
if (border_radius > 0) {
|
|
155
|
+
ctx.roundRect(
|
|
156
|
+
-border_half_width,
|
|
157
|
+
-border_half_width,
|
|
158
|
+
width + border_width,
|
|
159
|
+
height + border_width,
|
|
160
|
+
border_radius + border_half_width,
|
|
161
|
+
);
|
|
162
|
+
} else {
|
|
163
|
+
ctx.rect(
|
|
164
|
+
-border_half_width,
|
|
165
|
+
-border_half_width,
|
|
166
|
+
width + border_width,
|
|
167
|
+
height + border_width,
|
|
168
|
+
);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
ctx.closePath();
|
|
172
|
+
|
|
173
|
+
setStrokeStyle(ctx, rectangle);
|
|
174
|
+
ctx.lineWidth = border_width;
|
|
175
|
+
ctx.stroke();
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
ctx.restore();
|
|
179
|
+
} else {
|
|
180
|
+
let translated = false;
|
|
181
|
+
|
|
182
|
+
if (background !== null) {
|
|
183
|
+
const startColorStr = background.startColor._str;
|
|
184
|
+
|
|
185
|
+
if (startColorStr === background.endColor._str) {
|
|
186
|
+
// Solid background
|
|
187
|
+
ctx.beginPath();
|
|
188
|
+
|
|
189
|
+
if (border_radius > 0) {
|
|
190
|
+
ctx.roundRect(left, top, width, height, border_radius);
|
|
191
|
+
} else {
|
|
192
|
+
ctx.rect(left, top, width, height);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
ctx.closePath();
|
|
196
|
+
|
|
197
|
+
ctx.fillStyle = startColorStr;
|
|
198
|
+
ctx.fill();
|
|
199
|
+
} else {
|
|
200
|
+
// Gradient background
|
|
201
|
+
if (left !== 0 || top !== 0) {
|
|
202
|
+
ctx.translate(left, top);
|
|
203
|
+
}
|
|
204
|
+
translated = true;
|
|
205
|
+
|
|
206
|
+
// Prepare a rect path for the background and the clipping region
|
|
207
|
+
ctx.beginPath();
|
|
208
|
+
|
|
209
|
+
if (border_radius > 0) {
|
|
210
|
+
ctx.roundRect(0, 0, width, height, border_radius);
|
|
211
|
+
} else {
|
|
212
|
+
ctx.rect(0, 0, width, height);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
ctx.closePath();
|
|
216
|
+
|
|
217
|
+
setFillStyle(ctx, rectangle);
|
|
218
|
+
ctx.fill();
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
if (border_width > 0 && border_color !== null) {
|
|
223
|
+
const startColorStr = border_color.startColor._str;
|
|
224
|
+
|
|
225
|
+
if (startColorStr === border_color.endColor._str) {
|
|
226
|
+
// Solid border
|
|
227
|
+
ctx.beginPath();
|
|
228
|
+
|
|
229
|
+
if (translated) {
|
|
230
|
+
if (border_radius > 0) {
|
|
231
|
+
ctx.roundRect(
|
|
232
|
+
-border_half_width,
|
|
233
|
+
-border_half_width,
|
|
234
|
+
width + border_width,
|
|
235
|
+
height + border_width,
|
|
236
|
+
border_radius + border_half_width,
|
|
237
|
+
);
|
|
238
|
+
} else {
|
|
239
|
+
ctx.rect(
|
|
240
|
+
-border_half_width,
|
|
241
|
+
-border_half_width,
|
|
242
|
+
width + border_width,
|
|
243
|
+
height + border_width,
|
|
244
|
+
);
|
|
245
|
+
}
|
|
246
|
+
} else {
|
|
247
|
+
if (border_radius > 0) {
|
|
248
|
+
ctx.roundRect(
|
|
249
|
+
left - border_half_width,
|
|
250
|
+
top - border_half_width,
|
|
251
|
+
width + border_width,
|
|
252
|
+
height + border_width,
|
|
253
|
+
border_radius + border_half_width,
|
|
254
|
+
);
|
|
255
|
+
} else {
|
|
256
|
+
ctx.rect(
|
|
257
|
+
left - border_half_width,
|
|
258
|
+
top - border_half_width,
|
|
259
|
+
width + border_width,
|
|
260
|
+
height + border_width,
|
|
261
|
+
);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
ctx.closePath();
|
|
266
|
+
|
|
267
|
+
ctx.strokeStyle = startColorStr;
|
|
268
|
+
ctx.lineWidth = border_width;
|
|
269
|
+
ctx.stroke();
|
|
270
|
+
} else {
|
|
271
|
+
// Gradient border
|
|
272
|
+
if (!translated && (left !== 0 || top !== 0)) {
|
|
273
|
+
ctx.translate(left, top);
|
|
274
|
+
}
|
|
275
|
+
translated = true;
|
|
276
|
+
|
|
277
|
+
ctx.beginPath();
|
|
278
|
+
|
|
279
|
+
if (border_radius > 0) {
|
|
280
|
+
ctx.roundRect(
|
|
281
|
+
-border_half_width,
|
|
282
|
+
-border_half_width,
|
|
283
|
+
width + border_width,
|
|
284
|
+
height + border_width,
|
|
285
|
+
border_radius + border_half_width,
|
|
286
|
+
);
|
|
287
|
+
} else {
|
|
288
|
+
ctx.rect(
|
|
289
|
+
-border_half_width,
|
|
290
|
+
-border_half_width,
|
|
291
|
+
width + border_width,
|
|
292
|
+
height + border_width,
|
|
293
|
+
);
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
ctx.closePath();
|
|
297
|
+
|
|
298
|
+
setStrokeStyle(ctx, rectangle);
|
|
299
|
+
ctx.lineWidth = border_width;
|
|
300
|
+
ctx.stroke();
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
if (translated && (left !== 0 || top !== 0)) {
|
|
305
|
+
ctx.translate(-left, -top);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
if (blend_mode_changed) {
|
|
310
|
+
ctx.globalCompositeOperation = old_blend_mode;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
if (alpha_changed) {
|
|
314
|
+
ctx.globalAlpha = old_alpha;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
function setFillStyle(
|
|
320
|
+
ctx: CanvasRenderingContext2D,
|
|
321
|
+
rectangle: Rectangle,
|
|
322
|
+
): void {
|
|
323
|
+
const background = rectangle._background;
|
|
324
|
+
|
|
325
|
+
if (background !== null) {
|
|
326
|
+
const startColorStr = background.startColor._str;
|
|
327
|
+
const endColorStr = background.endColor._str;
|
|
328
|
+
|
|
329
|
+
if (startColorStr === endColorStr) {
|
|
330
|
+
// Solid background
|
|
331
|
+
ctx.fillStyle = startColorStr;
|
|
332
|
+
} else {
|
|
333
|
+
// Gradient background
|
|
334
|
+
const gradient_obj = fillMap.get(rectangle)!;
|
|
335
|
+
|
|
336
|
+
const trunc = Math.trunc;
|
|
337
|
+
const width = trunc(rectangle._width);
|
|
338
|
+
const height = trunc(rectangle._height);
|
|
339
|
+
const type = background.type;
|
|
340
|
+
|
|
341
|
+
if (
|
|
342
|
+
gradient_obj.gradient === null ||
|
|
343
|
+
gradient_obj.width !== width ||
|
|
344
|
+
gradient_obj.height !== height ||
|
|
345
|
+
gradient_obj.startColorStr !== startColorStr ||
|
|
346
|
+
gradient_obj.endColorStr !== endColorStr ||
|
|
347
|
+
gradient_obj.type !== type
|
|
348
|
+
) {
|
|
349
|
+
gradient_obj.width = width;
|
|
350
|
+
gradient_obj.height = height;
|
|
351
|
+
gradient_obj.startColorStr = startColorStr;
|
|
352
|
+
gradient_obj.endColorStr = endColorStr;
|
|
353
|
+
gradient_obj.type = type;
|
|
354
|
+
|
|
355
|
+
const end_x = type === "vertical" ? 0 : width;
|
|
356
|
+
const end_y = type === "vertical" ? height : 0;
|
|
357
|
+
const gradient = ctx.createLinearGradient(0, 0, end_x, end_y);
|
|
358
|
+
gradient.addColorStop(0, startColorStr);
|
|
359
|
+
gradient.addColorStop(1, endColorStr);
|
|
360
|
+
|
|
361
|
+
gradient_obj.gradient = gradient;
|
|
362
|
+
|
|
363
|
+
ctx.fillStyle = gradient;
|
|
364
|
+
} else {
|
|
365
|
+
ctx.fillStyle = gradient_obj.gradient;
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
function setStrokeStyle(
|
|
372
|
+
ctx: CanvasRenderingContext2D,
|
|
373
|
+
rectangle: Rectangle,
|
|
374
|
+
): void {
|
|
375
|
+
const border_color = rectangle._borderColor;
|
|
376
|
+
|
|
377
|
+
if (border_color !== null) {
|
|
378
|
+
const startColorStr = border_color.startColor._str;
|
|
379
|
+
const endColorStr = border_color.endColor._str;
|
|
380
|
+
|
|
381
|
+
if (startColorStr === endColorStr) {
|
|
382
|
+
// Solid border
|
|
383
|
+
ctx.strokeStyle = startColorStr;
|
|
384
|
+
} else {
|
|
385
|
+
// Gradient border
|
|
386
|
+
const gradient_obj = strokeMap.get(rectangle)!;
|
|
387
|
+
|
|
388
|
+
const trunc = Math.trunc;
|
|
389
|
+
const width = trunc(rectangle._width);
|
|
390
|
+
const height = trunc(rectangle._height);
|
|
391
|
+
const type = border_color.type;
|
|
392
|
+
|
|
393
|
+
if (
|
|
394
|
+
gradient_obj.gradient === null ||
|
|
395
|
+
gradient_obj.width !== width ||
|
|
396
|
+
gradient_obj.height !== height ||
|
|
397
|
+
gradient_obj.startColorStr !== startColorStr ||
|
|
398
|
+
gradient_obj.endColorStr !== endColorStr ||
|
|
399
|
+
gradient_obj.type !== type
|
|
400
|
+
) {
|
|
401
|
+
gradient_obj.width = width;
|
|
402
|
+
gradient_obj.height = height;
|
|
403
|
+
gradient_obj.startColorStr = startColorStr;
|
|
404
|
+
gradient_obj.endColorStr = endColorStr;
|
|
405
|
+
gradient_obj.type = type;
|
|
406
|
+
|
|
407
|
+
const end_x = type === "vertical" ? 0 : width;
|
|
408
|
+
const end_y = type === "vertical" ? height : 0;
|
|
409
|
+
const gradient = ctx.createLinearGradient(0, 0, end_x, end_y);
|
|
410
|
+
gradient.addColorStop(0, startColorStr);
|
|
411
|
+
gradient.addColorStop(1, endColorStr);
|
|
412
|
+
|
|
413
|
+
gradient_obj.gradient = gradient;
|
|
414
|
+
|
|
415
|
+
ctx.strokeStyle = gradient;
|
|
416
|
+
} else {
|
|
417
|
+
ctx.strokeStyle = gradient_obj.gradient;
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
}
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import type { Sprite } from "../Sprite.js";
|
|
2
|
+
import { playgroundMap } from "./renderPlayground.js";
|
|
3
|
+
|
|
4
|
+
export function initSprite(_sprite: Sprite): void {
|
|
5
|
+
// no-op
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export function removeSprite(_sprite: Sprite): void {
|
|
9
|
+
// no-op
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function drawSprite(sprite: Sprite, _interp: number): void {
|
|
13
|
+
const playground = sprite.playground!;
|
|
14
|
+
|
|
15
|
+
const left = sprite._drawLeft;
|
|
16
|
+
const top = sprite._drawTop;
|
|
17
|
+
const width = sprite._drawWidth;
|
|
18
|
+
const height = sprite._drawHeight;
|
|
19
|
+
|
|
20
|
+
const insidePlayground = playground._insidePlayground(
|
|
21
|
+
left,
|
|
22
|
+
top,
|
|
23
|
+
width,
|
|
24
|
+
height,
|
|
25
|
+
);
|
|
26
|
+
const animation = sprite._animation;
|
|
27
|
+
const opacity = sprite._opacity;
|
|
28
|
+
const scaleh = sprite._scaleh;
|
|
29
|
+
const scalev = sprite._scalev;
|
|
30
|
+
|
|
31
|
+
if (
|
|
32
|
+
insidePlayground &&
|
|
33
|
+
animation !== null &&
|
|
34
|
+
opacity !== 0 &&
|
|
35
|
+
scaleh !== 0 &&
|
|
36
|
+
scalev !== 0 &&
|
|
37
|
+
!sprite._hidden
|
|
38
|
+
) {
|
|
39
|
+
const playgroundData = playgroundMap.get(playground)!;
|
|
40
|
+
const ctx = playgroundData.ctx;
|
|
41
|
+
|
|
42
|
+
let old_alpha = 1;
|
|
43
|
+
let alpha_changed = false;
|
|
44
|
+
if (opacity !== 1) {
|
|
45
|
+
// Don't save the entire context only for opacity changes
|
|
46
|
+
old_alpha = ctx.globalAlpha;
|
|
47
|
+
|
|
48
|
+
ctx.globalAlpha = old_alpha * opacity;
|
|
49
|
+
|
|
50
|
+
alpha_changed = true;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
let old_blend_mode: GlobalCompositeOperation = "source-over";
|
|
54
|
+
let blend_mode_changed = false;
|
|
55
|
+
const blendMode = sprite._blendMode;
|
|
56
|
+
if (blendMode !== "normal") {
|
|
57
|
+
old_blend_mode = ctx.globalCompositeOperation;
|
|
58
|
+
|
|
59
|
+
if (blendMode === "add") {
|
|
60
|
+
ctx.globalCompositeOperation = "lighter";
|
|
61
|
+
} else {
|
|
62
|
+
ctx.globalCompositeOperation = blendMode;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
blend_mode_changed = true;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const angle = sprite._angle;
|
|
69
|
+
const sprite_sheet = animation.frameset[sprite._currentSpriteSheet]!;
|
|
70
|
+
const currentFrame = sprite._currentFrame;
|
|
71
|
+
|
|
72
|
+
if (angle !== 0 || scaleh !== 1 || scalev !== 1) {
|
|
73
|
+
const trunc = Math.trunc;
|
|
74
|
+
|
|
75
|
+
ctx.save();
|
|
76
|
+
|
|
77
|
+
let transformOriginx = sprite._transformOriginx;
|
|
78
|
+
|
|
79
|
+
if (typeof transformOriginx === "string") {
|
|
80
|
+
transformOriginx = sprite[transformOriginx];
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
transformOriginx = trunc(transformOriginx);
|
|
84
|
+
|
|
85
|
+
let transformOriginy = sprite._transformOriginy;
|
|
86
|
+
|
|
87
|
+
if (typeof transformOriginy === "string") {
|
|
88
|
+
transformOriginy = sprite[transformOriginy];
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
transformOriginy = trunc(transformOriginy);
|
|
92
|
+
|
|
93
|
+
ctx.translate(left + transformOriginx, top + transformOriginy);
|
|
94
|
+
|
|
95
|
+
if (angle !== 0) {
|
|
96
|
+
ctx.rotate(angle);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (scaleh !== 1 || scalev !== 1) {
|
|
100
|
+
ctx.scale(scaleh, scalev);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
ctx.translate(-transformOriginx, -transformOriginy);
|
|
104
|
+
|
|
105
|
+
ctx.drawImage(
|
|
106
|
+
sprite_sheet._img,
|
|
107
|
+
sprite_sheet.offsetx +
|
|
108
|
+
sprite._multix +
|
|
109
|
+
currentFrame * sprite_sheet._deltax,
|
|
110
|
+
sprite_sheet.offsety +
|
|
111
|
+
sprite._multiy +
|
|
112
|
+
currentFrame * sprite_sheet._deltay,
|
|
113
|
+
width,
|
|
114
|
+
height,
|
|
115
|
+
0,
|
|
116
|
+
0,
|
|
117
|
+
width,
|
|
118
|
+
height,
|
|
119
|
+
);
|
|
120
|
+
|
|
121
|
+
ctx.restore();
|
|
122
|
+
} else {
|
|
123
|
+
ctx.drawImage(
|
|
124
|
+
sprite_sheet._img,
|
|
125
|
+
sprite_sheet.offsetx +
|
|
126
|
+
sprite._multix +
|
|
127
|
+
currentFrame * sprite_sheet._deltax,
|
|
128
|
+
sprite_sheet.offsety +
|
|
129
|
+
sprite._multiy +
|
|
130
|
+
currentFrame * sprite_sheet._deltay,
|
|
131
|
+
width,
|
|
132
|
+
height,
|
|
133
|
+
left,
|
|
134
|
+
top,
|
|
135
|
+
width,
|
|
136
|
+
height,
|
|
137
|
+
);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
if (blend_mode_changed) {
|
|
141
|
+
ctx.globalCompositeOperation = old_blend_mode;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
if (alpha_changed) {
|
|
145
|
+
ctx.globalAlpha = old_alpha;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import {
|
|
2
|
+
drawGroupAfterChildren,
|
|
3
|
+
drawGroupBeforeChildren,
|
|
4
|
+
initGroup,
|
|
5
|
+
removeGroup,
|
|
6
|
+
} from "./canvasRenderer/renderGroup.js";
|
|
7
|
+
import {
|
|
8
|
+
drawPlaygroundAfterChildren,
|
|
9
|
+
drawPlaygroundBeforeChildren,
|
|
10
|
+
initPlayground,
|
|
11
|
+
} from "./canvasRenderer/renderPlayground.js";
|
|
12
|
+
import {
|
|
13
|
+
drawRectangle,
|
|
14
|
+
initRectangle,
|
|
15
|
+
removeRectangle,
|
|
16
|
+
} from "./canvasRenderer/renderRectangle.js";
|
|
17
|
+
import {
|
|
18
|
+
drawSprite,
|
|
19
|
+
initSprite,
|
|
20
|
+
removeSprite,
|
|
21
|
+
} from "./canvasRenderer/renderSprite.js";
|
|
22
|
+
import type { Renderer } from "./Renderer.js";
|
|
23
|
+
|
|
24
|
+
export const canvasRenderer: Renderer = {
|
|
25
|
+
initPlayground,
|
|
26
|
+
drawPlaygroundBeforeChildren,
|
|
27
|
+
drawPlaygroundAfterChildren,
|
|
28
|
+
initGroup,
|
|
29
|
+
removeGroup,
|
|
30
|
+
drawGroupBeforeChildren,
|
|
31
|
+
drawGroupAfterChildren,
|
|
32
|
+
initSprite,
|
|
33
|
+
removeSprite,
|
|
34
|
+
drawSprite,
|
|
35
|
+
initRectangle,
|
|
36
|
+
removeRectangle,
|
|
37
|
+
drawRectangle,
|
|
38
|
+
};
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type { Playground, PlaygroundOptions } from "./Playground.js";
|
|
2
|
+
import type { Rectangle } from "./Rectangle.js";
|
|
3
|
+
import type { Renderer } from "./Renderer.js";
|
|
4
|
+
import type { Sprite } from "./Sprite.js";
|
|
5
|
+
import type { SpriteGroup } from "./SpriteGroup.js";
|
|
6
|
+
|
|
7
|
+
export const dummyRenderer: Renderer = {
|
|
8
|
+
initPlayground(
|
|
9
|
+
_playground: Playground,
|
|
10
|
+
_options?: Partial<PlaygroundOptions>,
|
|
11
|
+
): [number, number] {
|
|
12
|
+
return [0, 0];
|
|
13
|
+
},
|
|
14
|
+
|
|
15
|
+
drawPlaygroundBeforeChildren(_playground: Playground, _interp: number): void {
|
|
16
|
+
// no-op
|
|
17
|
+
},
|
|
18
|
+
|
|
19
|
+
drawPlaygroundAfterChildren(_playground: Playground, _interp: number): void {
|
|
20
|
+
// no-op
|
|
21
|
+
},
|
|
22
|
+
|
|
23
|
+
initGroup(_group: SpriteGroup): void {
|
|
24
|
+
// no-op
|
|
25
|
+
},
|
|
26
|
+
|
|
27
|
+
removeGroup(_group: SpriteGroup): void {
|
|
28
|
+
// no-op
|
|
29
|
+
},
|
|
30
|
+
|
|
31
|
+
drawGroupBeforeChildren(_group: SpriteGroup, _interp: number): void {
|
|
32
|
+
// no-op
|
|
33
|
+
},
|
|
34
|
+
|
|
35
|
+
drawGroupAfterChildren(_group: SpriteGroup, _interp: number): void {
|
|
36
|
+
// no-op
|
|
37
|
+
},
|
|
38
|
+
|
|
39
|
+
initSprite(_sprite: Sprite): void {
|
|
40
|
+
// no-op
|
|
41
|
+
},
|
|
42
|
+
|
|
43
|
+
removeSprite(_sprite: Sprite): void {
|
|
44
|
+
// no-op
|
|
45
|
+
},
|
|
46
|
+
|
|
47
|
+
drawSprite(_sprite: Sprite, _interp: number): void {
|
|
48
|
+
// no-op
|
|
49
|
+
},
|
|
50
|
+
|
|
51
|
+
initRectangle(_rectangle: Rectangle): void {
|
|
52
|
+
// no-op
|
|
53
|
+
},
|
|
54
|
+
|
|
55
|
+
removeRectangle(_rectangle: Rectangle): void {
|
|
56
|
+
// no-op
|
|
57
|
+
},
|
|
58
|
+
|
|
59
|
+
drawRectangle(_rectangle: Rectangle, _interp: number): void {
|
|
60
|
+
// no-op
|
|
61
|
+
},
|
|
62
|
+
};
|