@litecanvas/plugin-migrate 0.0.1

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Luiz Bills
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,25 @@
1
+ # Migrate Plugin for Litecanvas
2
+
3
+ Migrate 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
+
5
+ ## Install
6
+
7
+ **NPM**: `npm i @litecanvas/plugin-migrate`
8
+
9
+ **CDN**: `https://unpkg.com/@litecanvas/plugin-migrate/dist/dist.js`
10
+
11
+ ## Usage
12
+
13
+ ```js
14
+ litecanvas({
15
+ loop: { init, draw },
16
+ })
17
+
18
+ use(pluginMigrate)
19
+
20
+ function draw() {
21
+ clear(0) // clear() was a alias for cls()
22
+
23
+ print(0, 0, "Hello World") // print() was a alias for text()
24
+ }
25
+ ```
package/dist/dist.js ADDED
@@ -0,0 +1,2 @@
1
+ (()=>{var v={mute:!1};function p(a,b=v){if(a.stat(1))throw'Plugin Migrate should be loaded before the "init" event';let l=a.stat(0);function n(t,e,s=""){b.mute||console.warn(`[Migrate] warning: ${t} is removed. `+(e?`Use ${e} instead. `:"")+s)}function d(t){return n("seed()","rseed()"),t&&a.rseed(t),a.stat(9)}let c="";function h(t){n("textstyle()","the 5th param of text()"),c=t}let g=a.text;function f(t,e,s,o=3,i=c){g(t,e,s,o,i)}function w(t,e,s,o){n("print()","text()"),f(t,e,s,o)}function _(t,e){n("textmetrics()","ctx().measureText()");let s=a.ctx(),o=a.stat(10),i=a.stat(11);s.font=`${c||""} ${~~(e||o)}px ${i}`;let m=s.measureText(t);return m.height=m.actualBoundingBoxAscent+m.actualBoundingBoxDescent,m}function A(t,e,s,o){n("cliprect()","clip()");let i=a.ctx();i.beginPath(),i.rect(t,e,s,o),i.clip()}function E(t,e,s){n("clipcirc()","clip()");let o=a.ctx();o.beginPath(),o.arc(t,e,s,0,a.TWO_PI),o.clip()}function C(t){n("getcolor()","stat(5)");let e=stat(5);return e[~~t%e.length]}function T(t){n("blendmode()","ctx().globalCompositeOperation");let e=a.ctx();e.globalCompositeOperation=t}function k(t){n("clear()","cls()"),a.cls(x,y,str,t)}function M(t,e,s,o,i,m,D=!0){return n("transform()","ctx().setTransform() or ctx().transform()"),a.ctx()[D?"setTransform":"transform"](t,e,s,o,i,m)}function Y(){return n("mousepos()","MX and MY"),[MX,MY]}function S(t){n("setfps()","framerate()"),a.framerate(t)}let r=a.def;function u(t,e){switch(t){case"W":case"WIDTH":r("W",e),r("WIDTH",e);break;case"H":case"HEIGHT":r("H",e),r("HEIGHT",e);break;case"T":case"ELAPSED":r("T",e),r("ELAPSED",e);break;case"CX":case"CENTERX":r("CX",e),r("CENTERX",e);break;case"CY":case"CENTERY":r("CY",e),r("CENTERY",e);break;case"MX":case"MOUSEX":r("MX",e),r("MOUSEX",e);break;case"MY":case"MOUSEY":r("MY",e),r("MOUSEY",e);break;default:r(t,e);break}}function X(t,e){n("setvar()","_core_def()"),u(t,e)}function P(t,e){if(l.autoscale)throw"resize() don't works with autoscale enabled";n("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(n("FPS","some library to measure the FPS"),r("FPS",""),l.fps&&a.framerate(l.fps),l.background>=0){let t=stat(5);a.CANVAS.style.backgroundColor=t[~~l.background%t.length]}return{def:u,seed:d,print:w,clear:k,setfps:S,setvar:X,textstyle:h,textmetrics:_,text:f,cliprect:A,clipcirc:E,blendmode:T,transform:M,getcolor:C,mousepos:Y,resize:P}}window.pluginMigrate=p;})();
2
+ /*! pluginMigrate for litecanvas v0.0.1 by Luiz Bills | MIT Licensed */
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "@litecanvas/plugin-migrate",
3
+ "version": "0.0.1",
4
+ "description": "Migrate 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
+ "author": "Luiz Bills <luizbills@pm.me>",
6
+ "license": "MIT",
7
+ "homepage": "https://github.com/litecanvas/plugin-migrate",
8
+ "bugs": {
9
+ "url": "https://github.com/litecanvas/plugin-migrate/issues"
10
+ },
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "git+https://github.com/litecanvas/plugin-migrate.git#main"
14
+ },
15
+ "main": "src/index.js",
16
+ "keywords": [
17
+ "litecanvas"
18
+ ],
19
+ "scripts": {
20
+ "dev": "esbuild src/_web.js --bundle --watch --outfile=dist/dist.js --servedir=.",
21
+ "build": "esbuild src/_web.js --bundle --minify --outfile=dist/dist.js",
22
+ "prepare": "npm run build"
23
+ },
24
+ "devDependencies": {
25
+ "esbuild": "^0.25.5"
26
+ },
27
+ "peerDependencies": {
28
+ "litecanvas": "latest"
29
+ },
30
+ "files": [
31
+ "dist",
32
+ "src"
33
+ ]
34
+ }
package/src/_web.js ADDED
@@ -0,0 +1,4 @@
1
+ import plugin from "."
2
+
3
+ /*! pluginMigrate for litecanvas v0.0.1 by Luiz Bills | MIT Licensed */
4
+ window.pluginMigrate = plugin
package/src/index.js ADDED
@@ -0,0 +1,219 @@
1
+ import "litecanvas"
2
+
3
+ const defaults = {
4
+ mute: false,
5
+ }
6
+
7
+ /**
8
+ *
9
+ * @param {LitecanvasInstance} engine
10
+ * @param {object} config
11
+ * @returns any
12
+ */
13
+ export default function plugin(engine, config = defaults) {
14
+ const initialized = engine.stat(1)
15
+
16
+ if (initialized) {
17
+ throw 'Plugin Migrate should be loaded before the "init" event'
18
+ }
19
+
20
+ const settings = engine.stat(0)
21
+
22
+ function warn(old, current, extra = "") {
23
+ if (!config.mute)
24
+ console.warn(
25
+ `[Migrate] warning: ${old} is removed. ` +
26
+ (current ? `Use ${current} instead. ` : "") +
27
+ extra
28
+ )
29
+ }
30
+
31
+ function seed(value) {
32
+ warn("seed()", "rseed()")
33
+ if (value) {
34
+ engine.rseed(value)
35
+ }
36
+ return engine.stat(9)
37
+ }
38
+
39
+ let _fontStyle = ""
40
+ function textstyle(value) {
41
+ warn("textstyle()", "the 5th param of text()")
42
+ _fontStyle = value
43
+ }
44
+
45
+ const _core_text = engine.text
46
+ function _text(x, y, str, color = 3, style = _fontStyle) {
47
+ _core_text(x, y, str, color, style)
48
+ }
49
+
50
+ function print(x, y, str, color) {
51
+ warn("print()", "text()")
52
+ _text(x, y, str, color)
53
+ }
54
+
55
+ function textmetrics(text, size) {
56
+ warn("textmetrics()", "ctx().measureText()")
57
+ const _ctx = engine.ctx()
58
+ const _fontSize = engine.stat(10)
59
+ const _fontFamily = engine.stat(11)
60
+ _ctx.font = `${_fontStyle || ""} ${~~(size || _fontSize)}px ${_fontFamily}`
61
+ const metrics = _ctx.measureText(text)
62
+ metrics.height =
63
+ metrics.actualBoundingBoxAscent + metrics.actualBoundingBoxDescent
64
+ return metrics
65
+ }
66
+
67
+ function cliprect(x, y, width, height) {
68
+ warn("cliprect()", "clip()")
69
+ const _ctx = engine.ctx()
70
+ _ctx.beginPath()
71
+ _ctx.rect(x, y, width, height)
72
+ _ctx.clip()
73
+ }
74
+
75
+ function clipcirc(x, y, radius) {
76
+ warn("clipcirc()", "clip()")
77
+ const _ctx = engine.ctx()
78
+ _ctx.beginPath()
79
+ _ctx.arc(x, y, radius, 0, engine.TWO_PI)
80
+ _ctx.clip()
81
+ }
82
+
83
+ function getcolor(index) {
84
+ warn("getcolor()", "stat(5)")
85
+ const colors = stat(5)
86
+ return colors[~~index % colors.length]
87
+ }
88
+
89
+ function blendmode(value) {
90
+ warn("blendmode()", "ctx().globalCompositeOperation")
91
+ const _ctx = engine.ctx()
92
+ _ctx.globalCompositeOperation = value
93
+ }
94
+
95
+ function clear(color) {
96
+ warn("clear()", "cls()")
97
+ engine.cls(x, y, str, color)
98
+ }
99
+
100
+ function transform(a, b, c, d, e, f, resetFirst = true) {
101
+ warn("transform()", "ctx().setTransform() or ctx().transform()")
102
+ const _ctx = engine.ctx()
103
+ return _ctx[resetFirst ? "setTransform" : "transform"](a, b, c, d, e, f)
104
+ }
105
+
106
+ function mousepos() {
107
+ warn("mousepos()", "MX and MY")
108
+ return [MX, MY]
109
+ }
110
+
111
+ function setfps(value) {
112
+ warn("setfps()", "framerate()")
113
+ engine.framerate(value)
114
+ }
115
+
116
+ const _core_def = engine.def
117
+ function _def(key, value) {
118
+ switch (key) {
119
+ case "W":
120
+ case "WIDTH":
121
+ _core_def("W", value)
122
+ _core_def("WIDTH", value)
123
+ break
124
+ case "H":
125
+ case "HEIGHT":
126
+ _core_def("H", value)
127
+ _core_def("HEIGHT", value)
128
+ break
129
+ case "T":
130
+ case "ELAPSED":
131
+ _core_def("T", value)
132
+ _core_def("ELAPSED", value)
133
+ break
134
+ case "CX":
135
+ case "CENTERX":
136
+ _core_def("CX", value)
137
+ _core_def("CENTERX", value)
138
+ break
139
+ case "CY":
140
+ case "CENTERY":
141
+ _core_def("CY", value)
142
+ _core_def("CENTERY", value)
143
+ break
144
+ case "MX":
145
+ case "MOUSEX":
146
+ _core_def("MX", value)
147
+ _core_def("MOUSEX", value)
148
+ break
149
+ case "MY":
150
+ case "MOUSEY":
151
+ _core_def("MY", value)
152
+ _core_def("MOUSEY", value)
153
+ break
154
+ default:
155
+ _core_def(key, value)
156
+ break
157
+ }
158
+ }
159
+
160
+ function setvar(key, value) {
161
+ warn("setvar()", "_core_def()")
162
+ _def(key, value)
163
+ }
164
+
165
+ function resize(width, height) {
166
+ if (settings.autoscale) {
167
+ throw "resize() don't works with autoscale enabled"
168
+ }
169
+
170
+ warn("resize()", null, "Avoid changing the canvas dimensions at runtime.")
171
+ engine.CANVAS.width = width
172
+ _def("W", width)
173
+ _def("CX", width / 2)
174
+
175
+ engine.CANVAS.height = height
176
+ _def("H", height)
177
+ _def("CY", height / 2)
178
+
179
+ engine.emit("resized", 1)
180
+ }
181
+
182
+ for (const key of ["W", "H", "T", "CX", "CY", "MX", "MY"]) {
183
+ if (null != engine[key]) {
184
+ _def(key, engine[key])
185
+ }
186
+ }
187
+
188
+ warn("FPS", "some library to measure the FPS")
189
+ _core_def("FPS", "")
190
+
191
+ if (settings.fps) {
192
+ engine.framerate(settings.fps)
193
+ }
194
+
195
+ if (settings.background >= 0) {
196
+ const colors = stat(5)
197
+ engine.CANVAS.style.backgroundColor =
198
+ colors[~~settings.background % colors.length]
199
+ }
200
+
201
+ return {
202
+ def: _def,
203
+ seed,
204
+ print,
205
+ clear,
206
+ setfps,
207
+ setvar,
208
+ textstyle,
209
+ textmetrics,
210
+ text: _text,
211
+ cliprect,
212
+ clipcirc,
213
+ blendmode,
214
+ transform,
215
+ getcolor,
216
+ mousepos,
217
+ resize,
218
+ }
219
+ }