canvas-poo 1.0.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/canvasPlus.js
ADDED
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
|
|
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
|
+
function canvasPOO(ctx) {
|
|
6
|
+
ctx.circle = (x, y, r) => {
|
|
7
|
+
ctx.beginPath(); ctx.arc(x, y, r, 0, Math.PI * 2); ctx.fill()
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
ctx.strokeCircle = (x, y, r) => {
|
|
11
|
+
ctx.beginPath(); ctx.arc(x, y, r, 0, Math.PI * 2); ctx.stroke()
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
ctx.roundRect = (x, y, w, h, r = 8) => {
|
|
15
|
+
ctx.beginPath()
|
|
16
|
+
ctx.moveTo(x + r, y)
|
|
17
|
+
ctx.lineTo(x + w - r, y)
|
|
18
|
+
ctx.arcTo(x + w, y, x + w, y + h, r)
|
|
19
|
+
ctx.arcTo(x + w, y + h, x, y + h, r)
|
|
20
|
+
ctx.arcTo(x, y + h, x, y, r)
|
|
21
|
+
ctx.arcTo(x, y, x + w, y, r)
|
|
22
|
+
ctx.closePath(); ctx.fill()
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
ctx.line = (x1, y1, x2, y2) => {
|
|
26
|
+
ctx.beginPath(); ctx.moveTo(x1, y1); ctx.lineTo(x2, y2); ctx.stroke()
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
ctx.polygon = (points) => {
|
|
30
|
+
if (!points.length) return
|
|
31
|
+
ctx.beginPath(); ctx.moveTo(points[0][0], points[0][1])
|
|
32
|
+
for (let i = 1; i < points.length; i++) ctx.lineTo(points[i][0], points[i][1])
|
|
33
|
+
ctx.closePath(); ctx.fill()
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
ctx.background = (color) => {
|
|
37
|
+
ctx.fillStyle = color; ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
ctx.clear = () => ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height)
|
|
41
|
+
|
|
42
|
+
ctx.print = (text, x, y, { size=16, family='sans-serif', color='#fff', align='left', baseline='top', bold=false } = {}) => {
|
|
43
|
+
ctx.save()
|
|
44
|
+
ctx.font = `${bold ? 'bold ' : ''}${size}px ${family}`
|
|
45
|
+
ctx.fillStyle = color; ctx.textAlign = align; ctx.textBaseline = baseline
|
|
46
|
+
ctx.fillText(text, x, y)
|
|
47
|
+
ctx.restore()
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
ctx.withAlpha = (a, fn) => { ctx.save(); ctx.globalAlpha = a; fn(); ctx.restore() }
|
|
51
|
+
|
|
52
|
+
ctx.linearGradient = (x1, y1, x2, y2, stops) => {
|
|
53
|
+
const g = ctx.createLinearGradient(x1, y1, x2, y2)
|
|
54
|
+
stops.forEach(([t, c]) => g.addColorStop(t, c)); return g
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
ctx.radialGradient = (x, y, r1, r2, stops) => {
|
|
58
|
+
const g = ctx.createRadialGradient(x, y, r1, x, y, r2)
|
|
59
|
+
stops.forEach(([t, c]) => g.addColorStop(t, c)); return g
|
|
60
|
+
}
|
|
61
|
+
|
|
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
|
+
})
|
|
84
|
+
|
|
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()
|
|
102
|
+
}
|
|
103
|
+
})
|
|
104
|
+
|
|
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
|
+
})
|
|
126
|
+
|
|
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,
|
|
136
|
+
draw() {
|
|
137
|
+
if (!this.visible || !this.img) return
|
|
138
|
+
ctx.save()
|
|
139
|
+
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)
|
|
144
|
+
ctx.restore()
|
|
145
|
+
}
|
|
146
|
+
})
|
|
147
|
+
|
|
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
|
+
}
|
|
173
|
+
})
|
|
174
|
+
|
|
175
|
+
return ctx
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
if (typeof module !== 'undefined') module.exports = canvasPOO
|
|
179
|
+
else window.canvasPlus = canvasPOO
|
package/package.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "canvas-poo",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "a javascript library that improves the canvas :D",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"canvas",
|
|
7
|
+
"graphics",
|
|
8
|
+
"poo",
|
|
9
|
+
"library"
|
|
10
|
+
],
|
|
11
|
+
"license": "ISC",
|
|
12
|
+
"author": "spacesxd",
|
|
13
|
+
"type": "commonjs",
|
|
14
|
+
"main": "canvasPOO.js",
|
|
15
|
+
"scripts": {
|
|
16
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
17
|
+
}
|
|
18
|
+
}
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|