Published
Edited
Nov 8, 2020
Insert cell
Insert cell
Insert cell
Insert cell
// =====================================
// write your own fragment shader below
// =====================================
fragShaderSource = `
precision highp float; // 声明float的精度
#define PI 3.14159265359
// JS与shader单向通信的变量
uniform vec2 u_size; // 当前画布的尺寸
uniform float u_dpr; // 绘制环境的devicePixelRatio

// 根据输入的value和目标value
// 返回位于目标点[0.0, +half_pi]范围内的突变值
float step_filter(float value, float taget_value){
return step(taget_value, value) -
step(taget_value + 0.2, value);
}

// color pattern
vec3 color_1 = vec3(45.0, 89, 198) / vec3(255.0);
vec3 color_2 = vec3(49, 142, 222) / vec3(255.0);
vec3 color_3 = vec3(38, 205, 213) / vec3(255.0);
vec3 color_4 = vec3(118, 224, 214) / vec3(255.0);

void main() {
vec2 st = gl_FragCoord.xy/u_size.xy;
vec3 color = vec3(0.0);

vec2 pos = vec2(0.5) - st; // 中心指向当前坐标点的向量
float distance = length(pos)*2.0; // 中心点到当前坐标点的距离

float alpha = atan(pos.y,pos.x); // 当前位置对应的旋转角
float f = tan(alpha * 5.); // 当前角度对应的函数值


color = mix(color, color_1, step_filter(distance, f)); // 比较实际距离 distance 和函数值 f 的大小
gl_FragColor = vec4(color, 1.0); // 进而影响当前像素的最终颜色
}
`
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
viewof s = {
return slider({
value: 400,
title: '画布大小',
min: 400,
max: 800,
step: 1,
})
};
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more