💡 Factor rendering logic out of chaos.js

This commit is contained in:
Mattéo Delabre 2015-12-23 15:53:33 +01:00
parent 58a4784a71
commit b8cef3d9bf
1 changed files with 2 additions and 17 deletions

View File

@ -1,7 +1,5 @@
'use strict';
import { getColor } from './utils';
/**
* Choose an index at random among a list of weights,
* more weighted indices have a greater proability to be chosen
@ -33,8 +31,7 @@ const chooseIndex = weights => {
* @param {Array} weights Probability weights for each transform
* @return {null}
*/
export const applyChaos = (image, start, iterations, transforms, weights) => {
const width = image.width;
export const applyChaos = (image, start, iterations, transforms, weights, cb) => {
let point = start;
if (weights === undefined) {
@ -45,20 +42,8 @@ export const applyChaos = (image, start, iterations, transforms, weights) => {
while (iterations--) {
const index = chooseIndex(weights);
const color = getColor(2);
// console.log(point);
// console.log(point.map(x => Math.floor(x * 10 + 100)));
point = transforms[index](point);
const i = (
Math.floor(point[1] * 50 + 50) * width +
Math.floor(point[0] * 50 + 200)
) * 4;
image.data[i] = color[0];
image.data[i + 1] = color[1];
image.data[i + 2] = color[2];
image.data[i + 3] = 255;
cb(point);
}
};