@litecanvas/plugin-migrate 0.2.1 → 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 CHANGED
@@ -1,2 +1,2 @@
1
- (()=>{var o=(a,c="Assertion failed")=>{if(!a)throw new Error(c)};var _=(a,c,m,l,s,h,f,b)=>(o(isFinite(a),"colrect: 1st param must be a number"),o(isFinite(c),"colrect: 2nd param must be a number"),o(isFinite(m),"colrect: 3rd param must be a number"),o(isFinite(l),"colrect: 4th param must be a number"),o(isFinite(s),"colrect: 5th param must be a number"),o(isFinite(h),"colrect: 6th param must be a number"),o(isFinite(f),"colrect: 7th param must be a number"),o(isFinite(b),"colrect: 8th param must be a number"),a<s+f&&a+m>s&&c<h+b&&c+l>h);var x=(a,c,m,l,s,h)=>(o(isFinite(a),"colcirc: 1st param must be a number"),o(isFinite(c),"colcirc: 2nd param must be a number"),o(isFinite(m),"colcirc: 3rd param must be a number"),o(isFinite(l),"colcirc: 4th param must be a number"),o(isFinite(s),"colcirc: 5th param must be a number"),o(isFinite(h),"colcirc: 6th param must be a number"),(l-a)*(l-a)+(s-c)*(s-c)<=(m+h)*(m+h));var wt=2*Math.PI;var oe=Math.PI/2;var Z={warnings:!0};function v(a,c={}){if(c=Object.assign({},Z,c),a.stat(1))throw'Plugin Migrate should be loaded before the "init" event';let l=a.stat(0);function s(t,e,r=""){c.warnings&&console.warn(`[litecanvas/migrate] ${t} is removed. `+(e?`Use ${e} instead. `:"")+r)}function h(t){return s("seed()","rseed()"),t&&a.rseed(t),a.stat(9)}let f="";function b(t){s("textstyle()","the 5th param of text()"),f=t}let w=a.text;function g(t,e,r,i=3,p=f){w(t,e,r,i,p)}function E(t,e,r,i){s("print()","text()"),g(t,e,r,i)}function M(t,e){s("textmetrics()","ctx().measureText()");let r=a.ctx(),i=a.stat(10),p=a.stat(11);r.font=`${f||""} ${~~(e||i)}px ${p}`;let d=r.measureText(t);return d.height=d.actualBoundingBoxAscent+d.actualBoundingBoxDescent,d}function I(t,e,r,i){s("cliprect()","clip()");let p=a.ctx();p.beginPath(),p.rect(t,e,r,i),p.clip()}function A(t,e,r){s("clipcirc()","clip()");let i=a.ctx();i.beginPath(),i.arc(t,e,r,0,a.TWO_PI),i.clip()}function T(t){s("getcolor()","stat(5)");let e=stat(5);return e[~~t%e.length]}function P(t){s("blendmode()","ctx().globalCompositeOperation");let e=a.ctx();e.globalCompositeOperation=t}function R(t){s("clear()","cls()"),a.cls(t)}function C(t,e,r,i,p,d,D=!0){return s("transform()","ctx().setTransform() or ctx().transform()"),a.ctx()[D?"setTransform":"transform"](t,e,r,i,p,d)}function S(){return s("mousepos()","MX and MY"),[MX,MY]}function k(t){s("setfps()","framerate()"),a.framerate(t)}let n=a.def;function u(t,e){switch(t){case"W":case"WIDTH":n("W",e),n("WIDTH",e);break;case"H":case"HEIGHT":n("H",e),n("HEIGHT",e);break;case"T":case"ELAPSED":n("T",e),n("ELAPSED",e);break;case"CX":case"CENTERX":n("CX",e),n("CENTERX",e);break;case"CY":case"CENTERY":n("CY",e),n("CENTERY",e);break;case"MX":case"MOUSEX":n("MX",e),n("MOUSEX",e);break;case"MY":case"MOUSEY":n("MY",e),n("MOUSEY",e);break;default:n(t,e);break}}function L(t,e){s("setvar()","def()"),u(t,e)}a.listen("resized",y);function y(){u("CX",a.W/2),u("CY",a.H/2)}y(),u("CANVAS",a.canvas());function N(t,e){if(l.autoscale)throw"resize() don't works with autoscale enabled";s("resize()",null,"Avoid changing the canvas dimensions at runtime."),a.CANVAS.width=t,u("W",t),u("CX",t/2),a.CANVAS.height=e,u("H",e),u("CY",e/2),a.emit("resized",1)}for(let t of["W","H","T","CX","CY","MX","MY"])a[t]!=null&&u(t,a[t]);if(s("FPS","","but you can use our plugin to measure the fps: https://github.com/litecanvas/plugin-frame-rate-meter"),n("FPS",""),l.fps&&a.framerate(l.fps),l.background!=null){let t=a.listen("before:draw",()=>{let e=stat(5);a.canvas().style.background=e[~~l.background%e.length],t()})}function O(t){return s("path()","`new Path2D`","See https://developer.mozilla.org/en-US/docs/Web/API/Path2D"),new Path2D(t)}let z=a.fill;function F(t,e){if(e instanceof Path2D){s("fill(color, path)");let r=a.stat(5),i=a.ctx();i.fillStyle=r[~~t%r.length],a.ctx().fill(e)}else z(t)}let Y=a.stroke;function H(t,e){if(e instanceof Path2D){s("stroke(color, path)");let r=a.stat(5),i=a.ctx();i.strokeStyle=r[~~t%r.length],a.ctx().stroke(e)}else Y(t)}let W=a.clip;function B(t){s("clip(path)","clip(callback)","E.g: `clip((ctx) => ctx.rect(0, 0, 200, 200))`"),t instanceof Path2D?a.ctx().clip(t):W(t)}return l.antialias&&s('"antialias" option','"pixelart" option'),{def:u,seed:h,print:E,clear:R,setfps:k,setvar:L,textstyle:b,textmetrics:M,text:g,cliprect:I,clipcirc:A,blendmode:P,transform:C,getcolor:T,mousepos:S,resize:N,path:O,fill:F,stroke:H,clip:B,colrect:_,colcirc:x}}window.pluginMigrate=v;})();
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.2.1",
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.8",
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
- _text(x, y, str, color)
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 != null) {
237
+ if (null != settings.background) {
238
+ warn('"background" option', "You must update your canvas CSS")
217
239
  const removeThisListener = engine.listen("before:draw", () => {
218
- const colors = stat(5)
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)
@@ -277,30 +297,36 @@ export default function plugin(engine, config = {}) {
277
297
  warn('"antialias" option', '"pixelart" option')
278
298
  }
279
299
 
280
- return {
281
- def: _def,
282
- seed,
283
- print,
284
- clear,
285
- setfps,
286
- setvar,
287
- textstyle,
288
- textmetrics,
289
- text: _text,
290
- cliprect,
291
- clipcirc,
292
- blendmode,
293
- transform,
294
- getcolor,
295
- mousepos,
296
- resize,
297
- path,
298
- fill,
299
- stroke,
300
- clip,
300
+ if (false === settings.pixelart) {
301
+ warn('"pixelart" option')
302
+ }
301
303
 
302
- // restore collision utils
303
- colrect,
304
- colcirc,
304
+ if (false === settings.animate) {
305
+ warn('"animate" option', "pause() in the of your draw()")
305
306
  }
307
+
308
+ const _core_paint = engine.paint
309
+ function paint(w, h, data, options) {
310
+ let cb = data
311
+ if (engine.spr && Array.isArray(data)) {
312
+ cb = () => {
313
+ engine.spr(0, 0, data.join("").replace(/ /g, "."))
314
+ }
315
+ }
316
+ return _core_paint(w, h, cb, options)
317
+ }
318
+
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
+ }
329
+ }
330
+
331
+ return replacements
306
332
  }