canvas-poo 1.0.2 → 1.0.3

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.
Files changed (2) hide show
  1. package/canvasPOO.js +75 -105
  2. package/package.json +1 -1
package/canvasPOO.js CHANGED
@@ -1,8 +1,12 @@
1
+ /**
2
+ canvasPOO.js
3
+ hecho por spacesxd
4
+ */
1
5
 
2
- /* --- canvasPOO ---
3
- hola soy spacesxd el creador de esta wea,
4
- si no sabes cómo usarlo puedes buscar en mi canal de Youtube o También en la documentación:) */
5
6
  function canvasPOO(ctx) {
7
+
8
+ // formas
9
+
6
10
  ctx.circle = (x, y, r) => {
7
11
  ctx.beginPath(); ctx.arc(x, y, r, 0, Math.PI * 2); ctx.fill()
8
12
  }
@@ -33,8 +37,13 @@ function canvasPOO(ctx) {
33
37
  ctx.closePath(); ctx.fill()
34
38
  }
35
39
 
40
+ // ── Helpers ──────────────────────────────────────────────
41
+
36
42
  ctx.background = (color) => {
37
- ctx.fillStyle = color; ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height)
43
+ ctx.save()
44
+ ctx.fillStyle = color
45
+ ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height)
46
+ ctx.restore()
38
47
  }
39
48
 
40
49
  ctx.clear = () => ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height)
@@ -59,121 +68,82 @@ function canvasPOO(ctx) {
59
68
  stops.forEach(([t, c]) => g.addColorStop(t, c)); return g
60
69
  }
61
70
 
62
- // objetos :D(POO para los "genios")
63
- ctx.Rect = (x, y, w, h, color = '#fff') => ({
64
- position: { x, y },
65
- size: { w, h },
66
- scale: { x: 1, y: 1 },
67
- rotation: 0,
68
- alpha: 1,
69
- visible: true,
70
- color,
71
- update: null,
72
- draw() {
73
- if (!this.visible) return
74
- ctx.save()
75
- ctx.globalAlpha = this.alpha
76
- ctx.translate(this.position.x + this.size.w / 2, this.position.y + this.size.h / 2)
77
- ctx.rotate(this.rotation)
78
- ctx.scale(this.scale.x, this.scale.y)
79
- ctx.fillStyle = this.color
80
- ctx.fillRect(-this.size.w / 2, -this.size.h / 2, this.size.w, this.size.h)
81
- ctx.restore()
82
- }
83
- })
71
+ //ciclos infinitos xd
84
72
 
85
- ctx.Circle = (x, y, r, color = '#fff') => ({
86
- position: { x, y },
87
- radius: r,
88
- scale: { x: 1, y: 1 },
89
- alpha: 1,
90
- visible: true,
91
- color,
92
- update: null,
93
- draw() {
94
- if (!this.visible) return
95
- ctx.save()
96
- ctx.globalAlpha = this.alpha
97
- ctx.translate(this.position.x, this.position.y)
98
- ctx.scale(this.scale.x, this.scale.y)
99
- ctx.fillStyle = this.color
100
- ctx.circle(0, 0, this.radius)
101
- ctx.restore()
73
+ ctx.loop = (fn) => {
74
+ let last = 0, time = 0, _raf
75
+ const tick = (now) => {
76
+ const dt = Math.min((now - last) / 1000, 0.05)
77
+ last = now; time += dt
78
+ fn(dt, time)
79
+ _raf = requestAnimationFrame(tick)
102
80
  }
103
- })
81
+ _raf = requestAnimationFrame((now) => { last = now; tick(now) })
82
+ return () => cancelAnimationFrame(_raf)
83
+ }
104
84
 
105
- ctx.Polygon = (points, color = '#fff') => ({
106
- position: { x: 0, y: 0 },
107
- scale: { x: 1, y: 1 },
108
- rotation: 0,
109
- alpha: 1,
110
- visible: true,
111
- color,
112
- points,
113
- update: null,
114
- draw() {
115
- if (!this.visible) return
116
- ctx.save()
117
- ctx.globalAlpha = this.alpha
118
- ctx.translate(this.position.x, this.position.y)
119
- ctx.rotate(this.rotation)
120
- ctx.scale(this.scale.x, this.scale.y)
121
- ctx.fillStyle = this.color
122
- ctx.polygon(this.points)
123
- ctx.restore()
124
- }
125
- })
85
+ // objetos 😼
126
86
 
127
- ctx.Sprite = (x, y, w, h, img) => ({
128
- position: { x, y },
129
- size: { w, h },
130
- scale: { x: 1, y: 1 },
131
- rotation: 0,
132
- alpha: 1,
133
- visible: true,
134
- img,
135
- update: null,
87
+ const makeObj = (drawFn, props) => ({
88
+ ...props,
89
+ visible: true,
90
+ alpha: 1,
136
91
  draw() {
137
- if (!this.visible || !this.img) return
92
+ if (!this.visible) return
138
93
  ctx.save()
139
94
  ctx.globalAlpha = this.alpha
140
- ctx.translate(this.position.x + this.size.w / 2, this.position.y + this.size.h / 2)
141
- ctx.rotate(this.rotation)
142
- ctx.scale(this.scale.x, this.scale.y)
143
- ctx.drawImage(this.img, -this.size.w / 2, -this.size.h / 2, this.size.w, this.size.h)
95
+ drawFn(this)
144
96
  ctx.restore()
145
97
  }
146
98
  })
147
99
 
148
- ctx.Text = (x, y, text, opts = {}) => ({
149
- position: { x, y },
150
- scale: { x: 1, y: 1 },
151
- text,
152
- size: opts.size ?? 16,
153
- family: opts.family ?? 'sans-serif',
154
- color: opts.color ?? '#fff',
155
- align: opts.align ?? 'left',
156
- baseline: opts.baseline ?? 'top',
157
- bold: opts.bold ?? false,
158
- alpha: 1,
159
- visible: true,
160
- update: null,
161
- draw() {
162
- if (!this.visible) return
163
- ctx.save()
164
- ctx.globalAlpha = this.alpha
165
- ctx.translate(this.position.x, this.position.y)
166
- ctx.scale(this.scale.x, this.scale.y)
167
- ctx.print(this.text, 0, 0, {
168
- size: this.size, family: this.family, color: this.color,
169
- align: this.align, baseline: this.baseline, bold: this.bold
170
- })
171
- ctx.restore()
172
- }
100
+ ctx.Rect = (x, y, w, h, color = '#fff') => makeObj(o => {
101
+ ctx.translate(o.position.x + o.size.w / 2, o.position.y + o.size.h / 2)
102
+ ctx.rotate(o.rotation)
103
+ ctx.scale(o.scale.x, o.scale.y)
104
+ ctx.fillStyle = o.color
105
+ ctx.fillRect(-o.size.w / 2, -o.size.h / 2, o.size.w, o.size.h)
106
+ }, { position:{x,y}, size:{w,h}, scale:{x:1,y:1}, rotation:0, color })
107
+
108
+ ctx.Circle = (x, y, r, color = '#fff') => makeObj(o => {
109
+ ctx.translate(o.position.x, o.position.y)
110
+ ctx.scale(o.scale.x, o.scale.y)
111
+ ctx.fillStyle = o.color
112
+ ctx.circle(0, 0, o.radius)
113
+ }, { position:{x,y}, radius:r, scale:{x:1,y:1}, color })
114
+
115
+ ctx.Polygon = (points, color = '#fff') => makeObj(o => {
116
+ ctx.translate(o.position.x, o.position.y)
117
+ ctx.rotate(o.rotation)
118
+ ctx.scale(o.scale.x, o.scale.y)
119
+ ctx.fillStyle = o.color
120
+ ctx.polygon(o.points)
121
+ }, { position:{x:0,y:0}, points, scale:{x:1,y:1}, rotation:0, color })
122
+
123
+ ctx.Sprite = (x, y, w, h, img) => makeObj(o => {
124
+ if (!o.img) return
125
+ ctx.translate(o.position.x + o.size.w / 2, o.position.y + o.size.h / 2)
126
+ ctx.rotate(o.rotation)
127
+ ctx.scale(o.scale.x, o.scale.y)
128
+ ctx.drawImage(o.img, -o.size.w / 2, -o.size.h / 2, o.size.w, o.size.h)
129
+ }, { position:{x,y}, size:{w,h}, scale:{x:1,y:1}, rotation:0, img })
130
+
131
+ ctx.Text = (x, y, text, opts = {}) => makeObj(o => {
132
+ ctx.translate(o.position.x, o.position.y)
133
+ ctx.scale(o.scale.x, o.scale.y)
134
+ ctx.print(o.text, 0, 0, {
135
+ size: o.size, family: o.family, color: o.color,
136
+ align: o.align, baseline: o.baseline, bold: o.bold
137
+ })
138
+ }, {
139
+ position:{x,y}, scale:{x:1,y:1}, text,
140
+ size: opts.size ?? 16, family: opts.family ?? 'sans-serif',
141
+ color: opts.color ?? '#fff', align: opts.align ?? 'left',
142
+ baseline: opts.baseline ?? 'top', bold: opts.bold ?? false
173
143
  })
174
144
 
175
145
  return ctx
176
146
  }
177
147
 
178
148
  if (typeof module !== 'undefined') module.exports = canvasPOO
179
- else window.canvasPlus = canvasPOO
149
+ else window.canvasPOO = canvasPOO
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "canvas-poo",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "a javascript library that improves the canvas :D",
5
5
  "keywords": [
6
6
  "canvas",