-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathsketch.js
139 lines (99 loc) · 3.57 KB
/
sketch.js
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
let buffer;
let shLinearMix, shSpectralMix;
let colorPicker1, colorPicker2;
let color1, color2;
function setup_shaders() {
let vertex = `
precision highp float;
attribute vec3 aPosition;
void main() {
vec4 positionVec4 = vec4(aPosition, 1.0);
positionVec4.xy = positionVec4.xy * 2.0 - 1.0;
gl_Position = positionVec4;
}
`;
let fragment_linear = `
precision highp float;
uniform vec2 u_resolution;
uniform vec4 u_color1;
uniform vec4 u_color2;
void main() {
vec2 st = gl_FragCoord.xy / u_resolution.xy;
st.y = 1.0 - st.y;
vec4 col = mix(u_color1, u_color2, st.x);
gl_FragColor = col;
}
`;
let fragment_spectral = `
precision highp float;
#include "spectral.glsl"
uniform vec2 u_resolution;
uniform vec4 u_color1;
uniform vec4 u_color2;
void main() {
vec2 st = gl_FragCoord.xy / u_resolution.xy;
st.y = 1.0 - st.y;
vec4 col = spectral_mix(u_color1, u_color2, st.x);
gl_FragColor = col;
}
`;
fragment_spectral = fragment_spectral.replace('#include "spectral.glsl"', spectral.glsl());
shLinearMix = buffer.createShader(vertex, fragment_linear);
shSpectralMix = buffer.createShader(vertex, fragment_spectral);
}
function setup() {
let canvas = createCanvas(800, 430);
canvas.parent('container');
pixelDensity(4);
buffer = createGraphics(800, 100, WEBGL);
//set to webgl2 for better precision
buffer.drawingContext = buffer.elt.getContext('webgl2');
buffer.setAttributes({ alpha: false });
buffer.pixelDensity(1);
buffer.noStroke();
buffer.noSmooth();
setup_shaders();
colorPicker1 = createColorPicker('#002185');
colorPicker1.parent('picker-1');
color1 = '';
colorPicker2 = createColorPicker('#fcd200');
colorPicker2.parent('picker-2');
color2 = '';
}
function draw() {
color1 = colorPicker1.color();
color2 = colorPicker2.color();
noStroke();
textSize(30);
fill('#555555');
textFont('Arial');
text('RGB mix', 0, 25);
buffer.shader(shLinearMix);
shLinearMix.setUniform('u_resolution', [buffer.width, buffer.height]);
shLinearMix.setUniform('u_color1', spectral.glsl_color(color1.levels));
shLinearMix.setUniform('u_color2', spectral.glsl_color(color2.levels));
buffer.rect(0, 0, buffer.width, buffer.height);
image(buffer, 0, 35, buffer.width, buffer.height, 0, 0, buffer.width, buffer.height);
text('Spectral.js mix (shader)', 0, 175);
buffer.clear();
buffer.shader(shSpectralMix);
shSpectralMix.setUniform('u_resolution', [buffer.width, buffer.height]);
shSpectralMix.setUniform('u_color1', spectral.glsl_color(color1.levels));
shSpectralMix.setUniform('u_color2', spectral.glsl_color(color2.levels));
buffer.rect(0, 0, buffer.width, buffer.height);
image(buffer, 0, 185, buffer.width, buffer.height, 0, 0, buffer.width, buffer.height);
buffer.clear();
text('Spectral.js mix (javascript)', 0, 325);
let steps = 8;
for (var i = 0; i <= steps; i++) {
let c = spectral.mix(color1.levels, color2.levels, i / steps);
fill(c);
rect(i * (width / (steps + 1)), 335, width / (steps + 1) + 1, 100);
}
let paletteElement = document.querySelector('#palette');
paletteElement.querySelectorAll('span').forEach((e) => e.remove());
let palette = spectral.palette(color1.levels, color2.levels, steps + 1);
palette.forEach((c) => {
paletteElement.appendChild(document.createElement('span')).textContent = c;
});
}