Skip to content

Commit 87ad60c

Browse files
committed
v.1.1.1, refactor methods
1 parent 2a9e695 commit 87ad60c

File tree

4 files changed

+247
-193
lines changed

4 files changed

+247
-193
lines changed

README.md

Lines changed: 99 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,33 @@
11
# Gradient
22

3-
Class to create **linear, radial, elliptical and conic gradients** as bitmaps, even without canvas
3+
Class to create **linear, radial, conic and elliptic gradients** as bitmaps without canvas
44

5-
**version 1.1.0** (11 kB minified)
5+
**version 1.1.1** (10 kB minified)
66

7-
Example:
7+
**API:**
8+
9+
```javascript
10+
// create gradients
11+
const grad = Gradient.createLinearGradient(x1, y1, x2, y2);
12+
const grad = Gradient.createRadialGradient(x1, y1, r1, x2, y2, r2);
13+
const grad = Gradient.createConicGradient(angle, cx, cy);
14+
const grad = Gradient.createEllipticGradient(cx, cy, rx, ry, angle);
15+
16+
// methods
17+
grad.addColorStop(offset, color);
18+
19+
grad.transform.scale(sx, sy);
20+
grad.transform.rotate(angle);
21+
grad.transform.translate(tx, ty);
22+
grad.transform.skewX(s);
23+
grad.transform.skewY(s);
24+
grad.transform.reset();
25+
26+
grad.getColorAt(x, y);
27+
grad.getBitmap(width, height);
28+
```
29+
30+
**Example:**
831

932
```javascript
1033
const w = 200, h = 200;
@@ -18,92 +41,113 @@ function addColorStops(gradient)
1841
});
1942
return gradient;
2043
}
21-
function applyGradient(gradient1, canvas1, gradient2, canvas2)
44+
function drawLinearGradient(x1, y1, x2, y2)
2245
{
46+
const canvas1 = document.getElementById('linear1');
47+
const canvas2 = document.getElementById('linear2');
48+
const ctx1 = canvas1.getContext("2d");
49+
const ctx2 = canvas2.getContext("2d");
50+
51+
const gradient1 = ctx1.createLinearGradient(x1, y1, x2, y2);
52+
const gradient2 = Gradient.createLinearGradient(x1, y1, x2, y2);
53+
2354
addColorStops(gradient1);
2455
addColorStops(gradient2);
25-
const ctx1 = canvas1.getContext("2d");
56+
2657
ctx1.fillStyle = gradient1;
2758
ctx1.fillRect(0, 0, w, h);
28-
const ctx2 = canvas2.getContext("2d");
59+
2960
const imData = ctx2.createImageData(w, h);
3061
imData.data.set(gradient2.getBitmap(w, h));
3162
ctx2.putImageData(imData, 0, 0);
3263
}
33-
function drawLinearGradient(x1, y1, x2, y2)
34-
{
35-
const canvas1 = document.getElementById('linear1');
36-
const canvas2 = document.getElementById('linear2');
37-
applyGradient(
38-
canvas1.getContext("2d").createLinearGradient(x1, y1, x2, y2),
39-
canvas1,
40-
Gradient.createLinearGradient(x1, y1, x2, y2),
41-
canvas2
42-
);
43-
}
4464
function drawRadialGradient(x1, y1, r1, x2, y2, r2)
4565
{
4666
const canvas1 = document.getElementById('radial1');
4767
const canvas2 = document.getElementById('radial2');
48-
applyGradient(
49-
canvas1.getContext("2d").createRadialGradient(x1, y1, r1, x2, y2, r2),
50-
canvas1,
51-
Gradient.createRadialGradient(x1, y1, r1, x2, y2, r2),
52-
canvas2
53-
);
68+
const ctx1 = canvas1.getContext("2d");
69+
const ctx2 = canvas2.getContext("2d");
70+
71+
const gradient1 = ctx1.createRadialGradient(x1, y1, r1, x2, y2, r2);
72+
const gradient2 = Gradient.createRadialGradient(x1, y1, r1, x2, y2, r2);
73+
74+
addColorStops(gradient1);
75+
addColorStops(gradient2);
76+
77+
ctx1.fillStyle = gradient1;
78+
ctx1.fillRect(0, 0, w, h);
79+
80+
const imData = ctx2.createImageData(w, h);
81+
imData.data.set(gradient2.getBitmap(w, h));
82+
ctx2.putImageData(imData, 0, 0);
5483
}
5584
function drawEllipticGradient(cx, cy, rx, ry, angle)
5685
{
5786
const canvas1 = document.getElementById('elliptic1');
5887
const canvas2 = document.getElementById('elliptic2');
5988
const canvas3 = document.getElementById('elliptic3');
6089
const canvas4 = document.getElementById('elliptic4');
61-
const r = Math.max(rx, ry), sx = rx/r, sy = ry/r;
6290
const ctx1 = canvas1.getContext("2d");
91+
const ctx2 = canvas2.getContext("2d");
6392
const ctx3 = canvas3.getContext("2d");
64-
// grad1 is a transformed radial gradient
65-
const grad1 = Gradient.createRadialGradient(cx, cy, 0, cx, cy, r);
66-
grad1.translate(-cx, -cy);
67-
grad1.scale(sx, sy);
68-
//grad1.rotate(-angle);
69-
grad1.translate(cx, cy);
70-
// grad2 is elliptic gradient
71-
const grad2 = Gradient.createEllipticGradient(cx, cy, rx, ry, angle);
72-
73-
applyGradient(
74-
ctx1.createRadialGradient(cx/sx, cy/sy, 0, cx/sx, cy/sy, r),
75-
canvas1,
76-
grad1,
77-
canvas2
78-
);
79-
// transform radial gradient to become an unrotated elliptic
93+
const ctx4 = canvas4.getContext("2d");
94+
95+
const r = Math.max(rx, ry), sx = rx/r, sy = ry/r;
96+
97+
const gradient1 = ctx1.createRadialGradient(cx/sx, cy/sy, 0, cx/sx, cy/sy, r);
98+
const gradient2 = Gradient.createRadialGradient(cx, cy, 0, cx, cy, r);
99+
const gradient3 = ctx3.createRadialGradient(cx, cy, 0, cx, cy, r);
100+
const gradient4 = Gradient.createEllipticGradient(cx, cy, rx, ry, angle);
101+
102+
addColorStops(gradient1);
103+
addColorStops(gradient2);
104+
addColorStops(gradient3);
105+
addColorStops(gradient4);
106+
107+
// transform radial gradient1 to become an unrotated elliptic
80108
ctx1.scale(sx, sy);
109+
ctx1.fillStyle = gradient1;
81110
ctx1.fillRect(0, 0, w/sx, h/sy);
111+
// gradient2 is a transformed radial gradient
112+
gradient2.transform.translate(-cx, -cy);
113+
gradient2.transform.scale(sx, sy);
114+
//gradient2.transform.rotate(-angle);
115+
gradient2.transform.translate(cx, cy);
116+
const imData2 = ctx2.createImageData(w, h);
117+
imData2.data.set(gradient2.getBitmap(w, h));
118+
ctx2.putImageData(imData2, 0, 0);
82119

83-
84-
applyGradient(
85-
ctx3.createRadialGradient(cx, cy, 0, cx, cy, r),
86-
canvas3,
87-
grad2,
88-
canvas4
89-
);
90-
// transform radial gradient to become a rotated elliptic (does not produce expected result)
91-
ctx3.translate(-cx, -cy);
120+
// transform radial gradient3 to become a rotated elliptic (does not produce expected result)
92121
ctx3.scale(sx, sy);
122+
ctx3.translate(-cx, -cy);
93123
ctx3.rotate(-angle);
94124
ctx3.translate(cx, cy);
125+
ctx3.fillStyle = gradient3;
95126
ctx3.fillRect(0, 0, w, h);
127+
// gradient4 is elliptic gradient
128+
const imData4 = ctx4.createImageData(w, h);
129+
imData4.data.set(gradient4.getBitmap(w, h));
130+
ctx4.putImageData(imData4, 0, 0);
96131
}
97132
function drawConicGradient(angle, cx, cy)
98133
{
99134
const canvas1 = document.getElementById('conic1');
100135
const canvas2 = document.getElementById('conic2');
101-
applyGradient(
102-
canvas1.getContext("2d").createConicGradient(angle, cx, cy),
103-
canvas1,
104-
Gradient.createConicGradient(angle, cx, cy),
105-
canvas2
106-
);
136+
const ctx1 = canvas1.getContext("2d");
137+
const ctx2 = canvas2.getContext("2d");
138+
139+
const gradient1 = ctx1.createConicGradient(angle, cx, cy);
140+
const gradient2 = Gradient.createConicGradient(angle, cx, cy);
141+
142+
addColorStops(gradient1);
143+
addColorStops(gradient2);
144+
145+
ctx1.fillStyle = gradient1;
146+
ctx1.fillRect(0, 0, w, h);
147+
148+
const imData = ctx2.createImageData(w, h);
149+
imData.data.set(gradient2.getBitmap(w, h));
150+
ctx2.putImageData(imData, 0, 0);
107151
}
108152

109153
drawLinearGradient(20, 20, w - 20, h - 20);

0 commit comments

Comments
 (0)