// Set up the canvas and WebGL
var canvas = document.getElementById('myCanvas');
var gl = canvas.getContext('webgl');
// Define the fractal
function fractal(minX, maxX, minY, maxY, resolution, iterations) {
var heart = [];
for (var x = minX; x <= maxX; x += resolution) {
for (var y = minY; y <= maxY; y += resolution) {
var isInside = isInside(x, y, minX, maxX, minY, maxY, resolution);
if (isInside) {
var color = color(x, y, iterations);
heart.push({x: x, y: y, color: color});
}
}
}
return heart;
}
function isInside(x, y, minX, maxX, minY, maxY, resolution) {
return x >= minX && x <= maxX && y >= minY && y <= maxY;
}
function color(x, y, iterations) {
var red = Math.sin((x + y + iterations) * 0.01);
var green = Math.sin((x - y + iterations) * 0.01);
var blue = Math.sin((x + y + iterations) * 0.01);
return {r: red, g: green, b: blue, a: 1.0};
}
// Generate the fractal
var heart = fractal(-250, 250, -250, 250, 50, 10);
// Draw the fractal
function drawHeart() {
gl.clearColor(0, 0, 0, 0);
gl.clear(gl.COLOR_BUFFER_BIT);
for (var i = 0; i < heart.length; i++) {
var vertex = heart[i];
drawPixel(vertex.x, vertex.y, vertex.color);
}
}
function drawPixel(x, y, color) {
var program = createProgram();
var positionAttributeLocation = gl.getAttribLocation(program, "a_position");
var colorUniformLocation = gl.getUniformLocation(program, "u_color");
// create buffer
var positionBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
// Put geometry data into buffer
setRectangle(x, y, 1, 1);
// Tell the attribute how to get data out of positionBuffer
gl.enableVertexAttribArray(positionAttributeLocation);
var size = 2; // 2 components per iteration
var type = gl.FLOAT; // the data is 32bit floats
var normalize = false; // don't normalize the data
var stride = 0; // 0 = move forward size * sizeof(type) each iteration to get the next position
var offset = 0; // start at the beginning of the buffer
gl.vertexAttribPointer(positionAttributeLocation, size, type, normalize, stride, offset);
// set the color
gl.uniform4fv(colorUniformLocation, [color.r, color.g, color.b, color.a]);
// draw
var primitiveType = gl.POINTS;
var offset = 0;
var count = 1;
gl.drawArrays(primitiveType, offset, count);
}
function setRectangle(x, y, width, height) {
var x1 = x;
var x2 = x + width;
var y1 = y;
var y2 = y + height;
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([
x1, y1,
x2, y1,
x1, y2,
x1, y2,
x2, y1,
x2, y2]), gl.STATIC_DRAW);
}
function createProgram() {
var vertexShaderSource = `
attribute vec2 a_position;
void main() {
gl_Position = vec4(a_position, 0, 1);
}
`;
var fragmentShaderSource = `
precision mediump float;
uniform vec4 u_color;
void main() {
gl_FragColor = u_color;
}
`;
// create vertex shader
var vertexShader = gl.createShader(gl.VERTEX_SHADER);
gl.shaderSource(vertexShader, vertexShaderSource);
gl.compileShader(vertexShader);
// create fragment shader
var fragmentShader = gl.createShader(gl.FRAGMENT_SHADER);
gl.shaderSource(fragmentShader, fragmentShaderSource);
gl.compileShader(fragmentShader);
// create program
var program = gl.createProgram();
gl.attachShader(program, vertexShader);
gl.attachShader(program, fragmentShader);
gl.linkProgram(program);
return program;
}
drawHeart();