💡 Factor rendering logic out of chaos.js
This commit is contained in:
parent
58a4784a71
commit
b8cef3d9bf
|
@ -1,7 +1,5 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
import { getColor } from './utils';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Choose an index at random among a list of weights,
|
* Choose an index at random among a list of weights,
|
||||||
* more weighted indices have a greater proability to be chosen
|
* 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
|
* @param {Array} weights Probability weights for each transform
|
||||||
* @return {null}
|
* @return {null}
|
||||||
*/
|
*/
|
||||||
export const applyChaos = (image, start, iterations, transforms, weights) => {
|
export const applyChaos = (image, start, iterations, transforms, weights, cb) => {
|
||||||
const width = image.width;
|
|
||||||
let point = start;
|
let point = start;
|
||||||
|
|
||||||
if (weights === undefined) {
|
if (weights === undefined) {
|
||||||
|
@ -45,20 +42,8 @@ export const applyChaos = (image, start, iterations, transforms, weights) => {
|
||||||
|
|
||||||
while (iterations--) {
|
while (iterations--) {
|
||||||
const index = chooseIndex(weights);
|
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);
|
point = transforms[index](point);
|
||||||
|
|
||||||
const i = (
|
cb(point);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue