import * as scrawl from '../source/scrawl.js';
import { reportSpeed } from './utilities.js';import * as scrawl from '../source/scrawl.js';
import { reportSpeed } from './utilities.js';const porthole = scrawl.findCanvas('porthole');
porthole.set({
css: {
borderRadius: '50%'
}
});Namespacing boilerplate
const namespace = porthole.name;
const name = (n) => `${namespace}-${n}`;const starling = scrawl.makeWheel({
name: name('starling'),
radius: 3,
handleX: 'center',
handleY: 'center',
method: 'fill',
fillStyle: 'white',Adding in some flags to help speed up the Display cycle
noDeltaUpdates: true,
noPositionDependencies: true,
noFilters: true,
noUserInteraction: true,
});Useful variables
let starCount = 0;
const addNumber = 100;Color factory will generate a new star color on each user click
const colorBuilder = scrawl.makeColor({
name: name('my-color-builder'),
minimumColor: '#fcc',
maximumColor: '#ccf',
colorSpace: 'OKLAB',
});makeStar function
const makeStars = function (buildNumber) {We use a Vector for calculating the new star’s direction
const v = scrawl.requestVector();
for (let i = 0; i < buildNumber; i++) {
starCount++;Clone the entity template
const star = starling.clone({
name: name(`star_${starCount}`),Additional flags for speeding up the Display cycle
noCanvasEngineUpdates: true,All cloned stars will share the template star’s State object - to save on memory
sharedState: true,
});
const myRandom = Math.random();
v.setXY(1, 0).rotate(Math.random() * 360).scalarMultiply(300); scrawl.makeTween({
name: star.name,
targets: star,
duration: Math.round((myRandom * 3000) + 2000),
cycles: 0,We will animate the star’s start Coordinate (using startX and startY pseudo-attributes)
definitions: [{
attribute: 'startX',
start: 300,
end: 300 + v.x
}, {
attribute: 'startY',
start: 300,
end: 300 + v.y
}, {We will also animate the star’s scale value to make the animation look more 3D
attribute: 'scale',
start: 0.5,
end: Math.round((1 - myRandom) * 0.9) + 0.6,
}]We start the Tween running immediately after it has been created.
}).run();
}We need to release our Vector back to its vector pool
scrawl.releaseVector(v);Change the color of the stars each time the user clicks on the porthole
starling.set({
fillStyle: colorBuilder.getRangeColor(Math.random()),
});
};Generate the initial stars
makeStars(100);Function to display frames-per-second data, and other information relevant to the demo
const report = reportSpeed('#reportmessage', function () {
return `Stars: ${starCount}`;
});Create the Display cycle animation
scrawl.makeRender({
name: name('animation'),
target: porthole,
afterShow: report,
});const addStars = (e) => {
e.preventDefault();
e.returnValue = false;
makeStars(addNumber);
};
scrawl.addNativeListener('click', addStars, porthole.domElement);console.log(scrawl.library);