@litecanvas/plugin-migrate 0.3.0 → 0.4.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/dist/dist.js +1 -1
- package/package.json +3 -3
- package/src/index.js +46 -40
package/dist/dist.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
(()=>{var
|
|
1
|
+
(()=>{var x=(s,c,h,m,n,r,b,f)=>s<n+b&&s+h>n&&c<r+f&&c+m>r;var _=(s,c,h,m,n,r)=>(m-s)*(m-s)+(n-c)*(n-c)<=(h+r)*(h+r);var At=2*Math.PI;var ge=Math.PI/2;var j={warnings:!0};function v(s,c={}){c=Object.assign({},j,c);let h=s.stat(1),m={def:u,seed:b,print:N,clear:T,setfps:S,setvar:R,textstyle:w,textmetrics:E,cliprect:M,clipcirc:I,blendmode:A,transform:k,getcolor:y,mousepos:P,resize:C,path:L,fill:Y,stroke:F,clip:X,paint:W,colrect:x,colcirc:_};if(h)throw'Plugin Migrate should be loaded before the "init" event';let n=s.stat(0);function r(t,e,a=""){c.warnings&&console.warn(`[litecanvas/migrate] ${t} is removed. `+(e?`Use ${e} instead. `:"")+a)}function b(t){return r("seed()","rseed()"),t&&s.rseed(t),s.stat(9)}let f="";function w(t){r("textstyle()","the 5th param of text()"),f=t}function N(t,e,a,i){r("print()","text()"),s.text(t,e,a,i)}function E(t,e){r("textmetrics()","ctx().measureText()");let a=s.ctx(),i=s.stat(10),l=s.stat(11);a.font=`${f||""} ${~~(e||i)}px ${l}`;let p=a.measureText(t);return p.height=p.actualBoundingBoxAscent+p.actualBoundingBoxDescent,p}function M(t,e,a,i){r("cliprect()","clip()");let l=s.ctx();l.beginPath(),l.rect(t,e,a,i),l.clip()}function I(t,e,a){r("clipcirc()","clip()");let i=s.ctx();i.beginPath(),i.arc(t,e,a,0,s.TWO_PI),i.clip()}function y(t){r("getcolor()","stat(5)");let e=stat(5);return e[~~t%e.length]}function A(t){r("blendmode()","ctx().globalCompositeOperation");let e=s.ctx();e.globalCompositeOperation=t}function T(t){r("clear()","cls()"),s.cls(t)}function k(t,e,a,i,l,p,B=!0){return r("transform()","ctx().setTransform() or ctx().transform()"),s.ctx()[B?"setTransform":"transform"](t,e,a,i,l,p)}function P(){return r("mousepos()","MX and MY"),[MX,MY]}function S(t){r("setfps()","framerate()"),s.framerate(t)}let o=s.def;function u(t,e){switch(t){case"W":case"WIDTH":o("W",e),o("WIDTH",e);break;case"H":case"HEIGHT":o("H",e),o("HEIGHT",e);break;case"T":case"ELAPSED":o("T",e),o("ELAPSED",e);break;case"CX":case"CENTERX":o("CX",e),o("CENTERX",e);break;case"CY":case"CENTERY":o("CY",e),o("CENTERY",e);break;case"MX":case"MOUSEX":o("MX",e),o("MOUSEX",e);break;case"MY":case"MOUSEY":o("MY",e),o("MOUSEY",e);break;default:o(t,e);break}}function R(t,e){r("setvar()","def()"),u(t,e)}s.listen("resized",g);function g(){u("CX",s.W/2),u("CY",s.H/2)}g(),u("CANVAS",s.canvas());function C(t,e){if(n.autoscale)throw"resize() don't works with autoscale enabled";r("resize()",null,"Avoid changing the canvas dimensions at runtime."),s.CANVAS.width=t,u("W",t),s.CANVAS.height=e,u("H",e),s.emit("resized",1)}for(let t of["W","H","T","CX","CY","MX","MY"])s[t]!=null&&u(t,s[t]);if(r("FPS","","but you can use our plugin to measure the fps: https://github.com/litecanvas/plugin-frame-rate-meter"),o("FPS",""),n.fps&&s.framerate(n.fps),n.background!=null){r('"background" option',"You must update your canvas CSS");let t=s.listen("before:draw",()=>{s.canvas().style.background=y(~~n.background),t()})}function L(t){return r("path()","`new Path2D()`","See https://developer.mozilla.org/en-US/docs/Web/API/Path2D"),new Path2D(t)}let z=s.fill;function Y(t,e){if(e instanceof Path2D){r("fill(color, path)");let a=s.stat(5),i=s.ctx();i.fillStyle=a[~~t%a.length],s.ctx().fill(e)}else z(t)}let O=s.stroke;function F(t,e){if(e instanceof Path2D){r("stroke(color, path)");let a=s.stat(5),i=s.ctx();i.strokeStyle=a[~~t%a.length],s.ctx().stroke(e)}else O(t)}let D=s.clip;function X(t){r("clip(path)","clip(callback)","E.g: `clip((ctx) => ctx.rect(0, 0, 200, 200))`"),t instanceof Path2D?s.ctx().clip(t):D(t)}n.antialias&&r('"antialias" option','"pixelart" option'),n.pixelart===!1&&r('"pixelart" option'),n.animate===!1&&r('"animate" option',"pause() in the of your draw()");let H=s.paint;function W(t,e,a,i){let l=a;return s.spr&&Array.isArray(a)&&(l=()=>{s.spr(0,0,a.join("").replace(/ /g,"."))}),H(t,e,l,i)}let d=s.spr;return d&&d.length===3&&(m.spr=function(t,e,a,i,l){Number.isFinite(a)&&a>0?(r("spr() width and height","spr(x, y, pixels)"),d(t,e,l)):d(t,e,a)}),m}window.pluginMigrate=v;})();
|
|
2
2
|
/*! Migrate for litecanvas by Luiz Bills | MIT Licensed */
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@litecanvas/plugin-migrate",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Sometimes upgrading litecanvas to a new version can be a lot of work. This plugin makes this easier, by restoring the Litecanvas APIs that were removed in newer versions, and additionally shows warnings in the browser console when removed and/or deprecated APIs are used.",
|
|
5
5
|
"author": "Luiz Bills <luizbills@pm.me>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -19,12 +19,12 @@
|
|
|
19
19
|
],
|
|
20
20
|
"scripts": {
|
|
21
21
|
"dev": "esbuild src/_web.js --bundle --watch --outfile=dist/dist.js --servedir=.",
|
|
22
|
-
"build": "esbuild src/_web.js --bundle --minify --outfile=dist/dist.js",
|
|
22
|
+
"build": "esbuild src/_web.js --drop-labels=DEV --bundle --minify --outfile=dist/dist.js",
|
|
23
23
|
"prepare": "npm run build",
|
|
24
24
|
"test": "ava --tap | tap-min"
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
27
|
-
"esbuild": "^0.25.
|
|
27
|
+
"esbuild": "^0.25.12",
|
|
28
28
|
"@litecanvas/jsdom-extras": "^1.1.0",
|
|
29
29
|
"@types/jsdom": "^21.1.7",
|
|
30
30
|
"ava": "^6.4.1",
|
package/src/index.js
CHANGED
|
@@ -16,6 +16,33 @@ export default function plugin(engine, config = {}) {
|
|
|
16
16
|
config = Object.assign({}, defaults, config)
|
|
17
17
|
const initialized = engine.stat(1)
|
|
18
18
|
|
|
19
|
+
const replacements = {
|
|
20
|
+
def: _def,
|
|
21
|
+
seed,
|
|
22
|
+
print,
|
|
23
|
+
clear,
|
|
24
|
+
setfps,
|
|
25
|
+
setvar,
|
|
26
|
+
textstyle,
|
|
27
|
+
textmetrics,
|
|
28
|
+
cliprect,
|
|
29
|
+
clipcirc,
|
|
30
|
+
blendmode,
|
|
31
|
+
transform,
|
|
32
|
+
getcolor,
|
|
33
|
+
mousepos,
|
|
34
|
+
resize,
|
|
35
|
+
path,
|
|
36
|
+
fill,
|
|
37
|
+
stroke,
|
|
38
|
+
clip,
|
|
39
|
+
paint,
|
|
40
|
+
|
|
41
|
+
// restore collision utils
|
|
42
|
+
colrect,
|
|
43
|
+
colcirc,
|
|
44
|
+
}
|
|
45
|
+
|
|
19
46
|
if (initialized) {
|
|
20
47
|
throw 'Plugin Migrate should be loaded before the "init" event'
|
|
21
48
|
}
|
|
@@ -46,14 +73,9 @@ export default function plugin(engine, config = {}) {
|
|
|
46
73
|
_fontStyle = value
|
|
47
74
|
}
|
|
48
75
|
|
|
49
|
-
const _core_text = engine.text
|
|
50
|
-
function _text(x, y, str, color = 3, style = _fontStyle) {
|
|
51
|
-
_core_text(x, y, str, color, style)
|
|
52
|
-
}
|
|
53
|
-
|
|
54
76
|
function print(x, y, str, color) {
|
|
55
77
|
warn("print()", "text()")
|
|
56
|
-
|
|
78
|
+
engine.text(x, y, str, color)
|
|
57
79
|
}
|
|
58
80
|
|
|
59
81
|
function textmetrics(text, size) {
|
|
@@ -184,13 +206,12 @@ export default function plugin(engine, config = {}) {
|
|
|
184
206
|
}
|
|
185
207
|
|
|
186
208
|
warn("resize()", null, "Avoid changing the canvas dimensions at runtime.")
|
|
209
|
+
|
|
187
210
|
engine.CANVAS.width = width
|
|
188
211
|
_def("W", width)
|
|
189
|
-
_def("CX", width / 2)
|
|
190
212
|
|
|
191
213
|
engine.CANVAS.height = height
|
|
192
214
|
_def("H", height)
|
|
193
|
-
_def("CY", height / 2)
|
|
194
215
|
|
|
195
216
|
engine.emit("resized", 1)
|
|
196
217
|
}
|
|
@@ -213,11 +234,10 @@ export default function plugin(engine, config = {}) {
|
|
|
213
234
|
}
|
|
214
235
|
|
|
215
236
|
// restore the "background" option
|
|
216
|
-
if (settings.background
|
|
237
|
+
if (null != settings.background) {
|
|
238
|
+
warn('"background" option', "You must update your canvas CSS")
|
|
217
239
|
const removeThisListener = engine.listen("before:draw", () => {
|
|
218
|
-
|
|
219
|
-
engine.canvas().style.background =
|
|
220
|
-
colors[~~settings.background % colors.length]
|
|
240
|
+
engine.canvas().style.background = getcolor(~~settings.background)
|
|
221
241
|
removeThisListener()
|
|
222
242
|
})
|
|
223
243
|
}
|
|
@@ -226,7 +246,7 @@ export default function plugin(engine, config = {}) {
|
|
|
226
246
|
function path(arg) {
|
|
227
247
|
warn(
|
|
228
248
|
"path()",
|
|
229
|
-
"`new Path2D`",
|
|
249
|
+
"`new Path2D()`",
|
|
230
250
|
"See https://developer.mozilla.org/en-US/docs/Web/API/Path2D"
|
|
231
251
|
)
|
|
232
252
|
return new Path2D(arg)
|
|
@@ -290,37 +310,23 @@ export default function plugin(engine, config = {}) {
|
|
|
290
310
|
let cb = data
|
|
291
311
|
if (engine.spr && Array.isArray(data)) {
|
|
292
312
|
cb = () => {
|
|
293
|
-
engine.spr(0, 0,
|
|
313
|
+
engine.spr(0, 0, data.join("").replace(/ /g, "."))
|
|
294
314
|
}
|
|
295
315
|
}
|
|
296
316
|
return _core_paint(w, h, cb, options)
|
|
297
317
|
}
|
|
298
318
|
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
cliprect,
|
|
310
|
-
clipcirc,
|
|
311
|
-
blendmode,
|
|
312
|
-
transform,
|
|
313
|
-
getcolor,
|
|
314
|
-
mousepos,
|
|
315
|
-
resize,
|
|
316
|
-
path,
|
|
317
|
-
fill,
|
|
318
|
-
stroke,
|
|
319
|
-
clip,
|
|
320
|
-
paint,
|
|
321
|
-
|
|
322
|
-
// restore collision utils
|
|
323
|
-
colrect,
|
|
324
|
-
colcirc,
|
|
319
|
+
const _core_spr = engine.spr
|
|
320
|
+
if (_core_spr && _core_spr.length === 3) {
|
|
321
|
+
replacements.spr = function (x, y, w, h, pixels) {
|
|
322
|
+
if (Number.isFinite(w) && w > 0) {
|
|
323
|
+
warn("spr() width and height", "spr(x, y, pixels)")
|
|
324
|
+
_core_spr(x, y, pixels)
|
|
325
|
+
} else {
|
|
326
|
+
_core_spr(x, y, w)
|
|
327
|
+
}
|
|
328
|
+
}
|
|
325
329
|
}
|
|
330
|
+
|
|
331
|
+
return replacements
|
|
326
332
|
}
|