-
Notifications
You must be signed in to change notification settings - Fork 90
/
gradientfill.test.ts
122 lines (114 loc) · 4.06 KB
/
gradientfill.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import { describe, expect, beforeEach, it } from "vitest";
import * as pureimage from "../src/index.js";
import { NAMED_COLORS } from "../src/named_colors";
import { save } from "./common";
describe("drawing gradients", () => {
let image;
let c;
let w = 200;
let h = 200;
beforeEach(() => {
image = pureimage.make(w, h);
c = image.getContext("2d");
c.fillStyle = "black";
c.fillRect(0, 0, w, h);
});
it("fillRect with linear gradient", async () => {
const grad = c.createLinearGradient(0, 0, w, h);
grad.addColorStop(0, "white");
grad.addColorStop(1, "blue");
c.fillStyle = grad;
c.fillRect(0, 0, w, h);
expect(image.getPixelRGBA(0, 0)).to.eq(0xffffffff);
// expect(image.getPixelRGBA(w-1, h-1)).to.eq(0x0000FFFF)
await save(image, "linear_gradient_fillrect");
});
it("fill with linear gradient", async () => {
c.imageSmoothingEnabled = true;
const grad = c.createLinearGradient(0, 0, 20, 20);
grad.addColorStop(0, "#ffffff");
grad.addColorStop(1, "blue");
c.fillStyle = grad;
c.beginPath();
c.rect(0, 0, 20, 20);
c.fill();
// expect(image.getPixelRGBA(0, 0)).to.eq(NAMED_COLORS.white)
expect(image.getPixelRGBA(19, 19)).to.eq(0x0c0cffff);
// console.log(image)
// for(let i=0; i<20; i++) {
// console.log(i, image.getPixelRGBA_separate(i,19))
// }
await save(image, "linear_gradient_fill.png");
});
it("linear gradient with multiple stops", async () => {
const grad = c.createLinearGradient(0, 0, w, 0);
grad.addColorStop(0, "#ff0000");
grad.addColorStop(0.5, "#00ff00");
grad.addColorStop(1, "#0000ff");
c.fillStyle = grad;
c.fillRect(0, 0, w, h);
await save(image, "linear_gradient_rgb_stops.png");
});
// it('stroke with linear gradient',async () => {
// c.imageSmoothingEnabled = true
// const grad = c.createLinearGradient(0, 0, 20, 20)
// grad.addColorStop(0, 'white')
// grad.addColorStop(1, 'blue')
// // c.fillStyle = grad
// // c.fillStyle = 'red'
// c.strokeStyle = grad
// // c.strokeStyle = 'red'
// c.lineWidth = 3
// c.beginPath()
// c.rect(5, 5, 10, 10)
// c.stroke()
//
// await save(image, 'linar_gradient_stroke.png')
// })
it("is making a radial gradient", async () => {
const grad = c.createRadialGradient(10, 10, 5, 10, 10, 10);
grad.addColorStop(0, "white");
grad.addColorStop(1, "green");
c.imageSmoothingEnabled = false;
c.fillStyle = grad;
c.fillRect(0, 0, 20, 20);
// expect(image.getPixelRGBA(0, 0)).to.eq(NAMED_COLORS.green)
// expect(image.getPixelRGBA(10, 10)).to.eq(NAMED_COLORS.white)
await save(image, "radial_gradient_fillrect.png");
});
it("is drawing a conical gradient with smooth colors", async () => {
const grad = c.createConicGradient(0, w / 2, h / 2);
grad.addColorStop(0, "white");
grad.addColorStop(1, "blue");
c.fillStyle = grad;
c.fillRect(0, 0, w, h);
await save(image, "conic_gradient_smooth");
});
it("is drawing a conical gradient with a different angle", async () => {
const grad = c.createConicGradient(Math.PI / 3, w / 2, h / 2);
grad.addColorStop(0, "white");
grad.addColorStop(1, "blue");
c.fillStyle = grad;
c.fillRect(0, 0, w, h);
await save(image, "conic_gradient_smooth_90");
});
it("is drawing a conical gradient with hard colors", async () => {
const grad = c.createConicGradient(Math.PI / 3, w / 2, h / 2);
grad.addColorStop(0, "#f00");
grad.addColorStop(0.2, "#00f");
grad.addColorStop(0.4, "#0ff");
grad.addColorStop(0.6, "#f0f");
grad.addColorStop(0.8, "#ff0");
grad.addColorStop(1, "#f00");
// grad.addColorStop(0, "#f00");
// grad.addColorStop(0.2, "#00f");
// grad.addColorStop(0.4, "#0ff");
// grad.addColorStop(0.6, "#f0f");
// grad.addColorStop(0.8, "#ff0");
// grad.addColorStop(1, "#f00");
c.fillStyle = grad;
c.fillRect(0, 0, w, h);
await save(image, "conic_gradient_smooth_rainbow");
});
it("is drawing a conical gradient with one color", async () => {});
});