chaos/scripts/index.js

119 lines
2.9 KiB
JavaScript
Raw Normal View History

'use strict';
2015-12-21 22:37:34 +00:00
import { html } from 'the-dom';
import { applyChaos } from './chaos';
2015-12-23 14:54:08 +00:00
import { barnsley } from './ifs';
2015-12-22 20:33:40 +00:00
const { body } = html(document);
2015-12-21 22:37:34 +00:00
const content = body.find('#content');
2015-12-22 20:33:40 +00:00
const plotting = body.find('#plotting').node;
const ctx = plotting.getContext('2d');
2015-12-23 14:54:08 +00:00
let dragging = false;
let center, zoom = 200;
let width, height;
let points = [[0, 0]];
/**
2015-12-22 20:33:40 +00:00
* Re-render the scene from scratch
*
2015-12-22 20:33:40 +00:00
* @return {null}
*/
2015-12-22 20:33:40 +00:00
const render = () => {
2015-12-23 14:54:08 +00:00
plotting.width = width;
plotting.height = height;
2015-12-22 20:33:40 +00:00
// do not plot (very) small sizes
if (width < 1) {
return;
}
2015-12-22 20:33:40 +00:00
// do the chaos game
2015-12-23 14:54:08 +00:00
const image = ctx.getImageData(0, 0, width, height);
const length = points.length;
2015-12-23 14:54:08 +00:00
const color = [0, 0, 0];
for (let i = 50; i < length; i += 1) {
const x = Math.floor(points[i][0] * zoom + center[0]);
const y = height - Math.floor(points[i][1] * zoom + center[1]);
2015-12-23 14:54:08 +00:00
if (x >= 0 && x < width && y >= 0 && y < height) {
const index = (y * width + x) * 4;
2015-12-22 20:33:40 +00:00
image.data[index] = color[0];
image.data[index + 1] = color[1];
image.data[index + 2] = color[2];
image.data[index + 3] = 255;
2015-12-23 14:54:08 +00:00
}
}
2015-12-23 14:54:08 +00:00
ctx.putImageData(image, 0, 0);
};
/**
2015-12-22 20:33:40 +00:00
* Update the scene when the window has been resized
*
* @return {null}
*/
const resize = () => {
2015-12-23 14:54:08 +00:00
width = content.node.clientWidth;
height = content.node.clientHeight;
center = [Math.floor(width / 2), Math.floor(height / 2)];
render();
};
2015-12-23 14:54:08 +00:00
/**
* Zoom on the cursor position when using mouse wheel
*/
content.on('wheel', event => {
const delta = event.deltaMode === 0 ? event.deltaY / 53 : event.deltaY;
const newZoom = zoom * Math.max(0, 1 - delta * .035);
2015-12-23 14:54:08 +00:00
// which (unprojected) point does the mouse point on?
const mouse = [
(event.offsetX - center[0]) / zoom,
(height - event.offsetY - center[1]) / zoom
];
// we need to set the center so that `mouse` stays at
// the same position on screen, i.e (vectorially):
// mouse * newZoom + newCenter = mouse * zoom + center
// => newCenter = mouse * zoom - mouse * newZoom + center
center = [
mouse[0] * zoom - mouse[0] * newZoom + center[0],
mouse[1] * zoom - mouse[1] * newZoom + center[1]
];
zoom = newZoom;
render();
event.preventDefault();
});
/**
* Pan the content with click-drag action
*/
content.on('mousedown', event => dragging = [event.offsetX, event.offsetY]);
content.on('mouseup', () => dragging = false);
content.on('mousemove', event => {
if (dragging !== false) {
const newMouse = [event.offsetX, event.offsetY];
const movement = [newMouse[0] - dragging[0], newMouse[1] - dragging[1]];
center[0] += movement[0];
center[1] -= movement[1];
render();
dragging = newMouse;
event.preventDefault();
}
});
applyChaos(points, 200000, ...barnsley);
window.onresize = resize;
resize();