@litecanvas/plugin-migrate 0.4.1 → 0.6.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/README.md +13 -2
- package/dist/dist.js +2 -2
- package/package.json +6 -2
- package/src/index.js +52 -17
package/README.md
CHANGED
|
@@ -2,11 +2,22 @@
|
|
|
2
2
|
|
|
3
3
|
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.
|
|
4
4
|
|
|
5
|
+
<!-- prettier-ignore -->
|
|
6
|
+
> [!TIP]
|
|
7
|
+
> **This plugin is automatically loaded on Litecanvas [playground](https://litecanvas.js.org/) to maintain compatibility and avoid breaking code written for older versions.**
|
|
8
|
+
|
|
5
9
|
## Install
|
|
6
10
|
|
|
7
|
-
|
|
11
|
+
### NPM
|
|
12
|
+
|
|
13
|
+
```
|
|
14
|
+
npm i @litecanvas/plugin-migrate
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
### CDN
|
|
8
18
|
|
|
9
|
-
|
|
19
|
+
- Unpkg: `https://unpkg.com/@litecanvas/plugin-migrate/dist/dist.js`
|
|
20
|
+
- jsDelivr: `https://cdn.jsdelivr.net/npm/@litecanvas/plugin-migrate/dist/dist.js`
|
|
10
21
|
|
|
11
22
|
## Usage
|
|
12
23
|
|
package/dist/dist.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
(()=>{var
|
|
2
|
-
`))}),
|
|
1
|
+
(()=>{var _=(r,u,p,m,n,f,b,x)=>r<n+b&&r+p>n&&u<f+x&&u+m>f;var y=(r,u,p,m,n,f)=>(m-r)*(m-r)+(n-u)*(n-u)<=(p+f)*(p+f);var Bt=2*Math.PI;var N=(r,u,p,m=Math.sin)=>r+(m(p)+1)/2*(u-r);var vr=Math.PI/2;var tt={warnings:!0};function v(r,u={}){if(r.stat(1))throw'Plugin Migrate should be loaded before the "init" event';u=Object.assign({},tt,u);let m={def:c,seed:f,print:T,clear:R,setfps:z,setvar:Y,textstyle:x,textmetrics:I,cliprect:k,clipcirc:F,blendmode:S,transform:C,getcolor:w,mousepos:L,resize:O,path:H,fill:B,stroke:D,clip:V,paint:$,colrect:(...t)=>(g("colrect()"),_(...t)),colcirc:(...t)=>(g("colrect()"),y(...t)),wave:(...t)=>(g("wave()"),N(...t))},n=r.stat(0);function f(t){return a("seed()","rseed()"),t&&r.rseed(t),r.stat(9)}let b="";function x(t){a("textstyle()","the 5th param of text()"),b=t}function T(t,e,s,i){a("print()","text()"),r.text(t,e,s,i)}function I(t,e){a("textmetrics()","ctx().measureText()");let s=r.ctx(),i=r.stat(10),l=r.stat(11);s.font=`${b||""} ${~~(e||i)}px ${l}`;let h=s.measureText(t);return h.height=h.actualBoundingBoxAscent+h.actualBoundingBoxDescent,h}function k(t,e,s,i){a("cliprect()","clip()");let l=r.ctx();l.beginPath(),l.rect(t,e,s,i),l.clip()}function F(t,e,s){a("clipcirc()","clip()");let i=r.ctx();i.beginPath(),i.arc(t,e,s,0,r.TWO_PI),i.clip()}function w(t){a("getcolor()","stat(5)");let e=stat(5);return e[~~t%e.length]}function S(t){a("blendmode()","ctx().globalCompositeOperation");let e=r.ctx();e.globalCompositeOperation=t}function R(t){a("clear()","cls()"),r.cls(t)}function C(t,e,s,i,l,h,j=!0){return a("transform()","ctx().setTransform() or ctx().transform()"),r.ctx()[j?"setTransform":"transform"](t,e,s,i,l,h)}function L(){return a("mousepos()","MX and MY"),[MX,MY]}function z(t){a("setfps()","framerate()"),r.framerate(t)}let o=r.def;function c(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 Y(t,e){a("setvar()","def()"),c(t,e)}r.listen("resized",E);function E(){c("CX",r.W/2),c("CY",r.H/2)}E(),c("CANVAS",r.canvas());function O(t,e){if(n.autoscale)throw"resize() don't works with autoscale enabled";a("resize()",null,"Avoid changing the canvas dimensions at runtime."),r.CANVAS.width=t,c("W",t),r.CANVAS.height=e,c("H",e),r.emit("resized",1)}for(let t of["W","H","T","CX","CY","MX","MY"])r[t]!=null&&c(t,r[t]);if(a("FPS","","but you can use our plugin to measure the fps: https://github.com/litecanvas/plugin-frame-rate-meter"),o("FPS",""),n.fps&&r.framerate(n.fps),n.background!=null){a('"background" option',"You must update your canvas CSS");let t=r.listen("before:draw",()=>{r.canvas().style.background=w(~~n.background),t()})}function H(t){return a("path()","`new Path2D()`","See https://developer.mozilla.org/en-US/docs/Web/API/Path2D"),new Path2D(t)}let X=r.fill;function B(t,e){if(e instanceof Path2D){a("fill(color, path)");let s=r.stat(5),i=r.ctx();i.fillStyle=s[~~t%s.length],r.ctx().fill(e)}else X(t)}let W=r.stroke;function D(t,e){if(e instanceof Path2D){a("stroke(color, path)");let s=r.stat(5),i=r.ctx();i.strokeStyle=s[~~t%s.length],r.ctx().stroke(e)}else W(t)}let U=r.clip;function V(t){a("clip(path)","clip(callback)","E.g: `clip((ctx) => ctx.rect(0, 0, 200, 200))`"),t instanceof Path2D?r.ctx().clip(t):U(t)}n.antialias&&a('"antialias" option','"pixelart" option'),n.pixelart===!1&&a('"pixelart" option'),n.animate===!1&&a('"animate" option',"pause() in the of your draw()");let Z=r.paint;function $(t,e,s,i){let l=s;return r.spr&&Array.isArray(s)&&(l=()=>{r.spr(0,0,s.join(`
|
|
2
|
+
`))}),Z(t,e,l,i)}let d=r.spr;d&&d.length===3&&(m.spr=function(t,e,s,i,l){Number.isFinite(s)&&s>0?(a("spr() width and height","spr(x, y, pixels)"),d(t,e,l)):d(t,e,s)});let M=r.unlisten;if(M){let t=r.listen;m.listen=(e,s)=>(t(e,s),()=>{A("listen() not returns a function anymore. Please use unlisten(event, callback) instead"),M(e,s)})}function A(t){u.warnings&&console.warn(`[litecanvas/migrate] ${t}.`)}function a(t,e,s=""){A(`${t} is removed. `+(e?`Please use ${e} instead. `:"")+s)}function g(t,e="function"){a(t,`This ${e} was moved to @litecanvas/utils package.`)}return m}window.pluginMigrate=v;})();
|
|
3
3
|
/*! 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.6.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",
|
|
@@ -14,8 +14,12 @@
|
|
|
14
14
|
},
|
|
15
15
|
"type": "module",
|
|
16
16
|
"main": "src/index.js",
|
|
17
|
+
"unpkg": "./dist/dist.js",
|
|
18
|
+
"browser": "./dist/dist.js",
|
|
17
19
|
"keywords": [
|
|
18
|
-
"litecanvas"
|
|
20
|
+
"litecanvas",
|
|
21
|
+
"migration",
|
|
22
|
+
"compatibility"
|
|
19
23
|
],
|
|
20
24
|
"scripts": {
|
|
21
25
|
"dev": "esbuild src/_web.js --bundle --watch --outfile=dist/dist.js --servedir=.",
|
package/src/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import "litecanvas"
|
|
2
2
|
|
|
3
|
-
import { colrect, colcirc } from "@litecanvas/utils"
|
|
3
|
+
import { colrect, colcirc, wave } from "@litecanvas/utils"
|
|
4
4
|
|
|
5
5
|
const defaults = {
|
|
6
6
|
warnings: true,
|
|
@@ -13,9 +13,14 @@ const defaults = {
|
|
|
13
13
|
* @returns any
|
|
14
14
|
*/
|
|
15
15
|
export default function plugin(engine, config = {}) {
|
|
16
|
-
config = Object.assign({}, defaults, config)
|
|
17
16
|
const initialized = engine.stat(1)
|
|
18
17
|
|
|
18
|
+
if (initialized) {
|
|
19
|
+
throw 'Plugin Migrate should be loaded before the "init" event'
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
config = Object.assign({}, defaults, config)
|
|
23
|
+
|
|
19
24
|
const replacements = {
|
|
20
25
|
def: _def,
|
|
21
26
|
seed,
|
|
@@ -39,26 +44,23 @@ export default function plugin(engine, config = {}) {
|
|
|
39
44
|
paint,
|
|
40
45
|
|
|
41
46
|
// restore collision utils
|
|
42
|
-
colrect
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
47
|
+
colrect: (...args) => {
|
|
48
|
+
warnUtils("colrect()")
|
|
49
|
+
return colrect(...args)
|
|
50
|
+
},
|
|
51
|
+
colcirc: (...args) => {
|
|
52
|
+
warnUtils("colrect()")
|
|
53
|
+
return colcirc(...args)
|
|
54
|
+
},
|
|
55
|
+
wave: (...args) => {
|
|
56
|
+
warnUtils("wave()")
|
|
57
|
+
return wave(...args)
|
|
58
|
+
},
|
|
48
59
|
}
|
|
49
60
|
|
|
50
61
|
/** @type {LitecanvasOptions} */
|
|
51
62
|
const settings = engine.stat(0)
|
|
52
63
|
|
|
53
|
-
function warn(old, current, extra = "") {
|
|
54
|
-
if (config.warnings)
|
|
55
|
-
console.warn(
|
|
56
|
-
`[litecanvas/migrate] ${old} is removed. ` +
|
|
57
|
-
(current ? `Use ${current} instead. ` : "") +
|
|
58
|
-
extra
|
|
59
|
-
)
|
|
60
|
-
}
|
|
61
|
-
|
|
62
64
|
function seed(value) {
|
|
63
65
|
warn("seed()", "rseed()")
|
|
64
66
|
if (value) {
|
|
@@ -328,5 +330,38 @@ export default function plugin(engine, config = {}) {
|
|
|
328
330
|
}
|
|
329
331
|
}
|
|
330
332
|
|
|
333
|
+
// restore `listen()` pre v0.200
|
|
334
|
+
const _core_unlisten = engine.unlisten
|
|
335
|
+
if (_core_unlisten) {
|
|
336
|
+
const _core_listen = engine.listen
|
|
337
|
+
replacements.listen = (eventName, callback) => {
|
|
338
|
+
_core_listen(eventName, callback)
|
|
339
|
+
return () => {
|
|
340
|
+
warnMessage(
|
|
341
|
+
"listen() not returns a function anymore. Please use unlisten(event, callback) instead"
|
|
342
|
+
)
|
|
343
|
+
_core_unlisten(eventName, callback)
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
function warnMessage(message) {
|
|
349
|
+
if (config.warnings) {
|
|
350
|
+
console.warn(`[litecanvas/migrate] ${message}.`)
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
function warn(old, current, extra = "") {
|
|
355
|
+
warnMessage(
|
|
356
|
+
`${old} is removed. ` +
|
|
357
|
+
(current ? `Please use ${current} instead. ` : "") +
|
|
358
|
+
extra
|
|
359
|
+
)
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
function warnUtils(func, type = "function") {
|
|
363
|
+
warn(func, `This ${type} was moved to @litecanvas/utils package.`)
|
|
364
|
+
}
|
|
365
|
+
|
|
331
366
|
return replacements
|
|
332
367
|
}
|