// declare a variable that will be used to store the raster
let raster;
// create a group that will contain the points
// it will be used to easily remove all created points
const points = new Group();
// create tool
const panHandler = new Tool();
panHandler.onMouseDrag = function(event) {
var offset = event.downPoint - event.point;
var newCenter = view.center.clone() + offset;
view.center = newCenter;
};
panHandler.onKeyDown = function(event) {
if (event.key == 'a') {
for (var i = 0; i < 1000; i++) {
addRandomPoint();
}
rasterize();
}
};
// create counter text
const counter = new PointText({
point: [50, 50],
content: '0',
fillColor: 'black',
fontFamily: 'Courier New',
fontWeight: 'bold',
fontSize: 50,
strokeColor: 'white',
strokeWidth: 1
});
addStats();
//
// METHODS
//
function addRandomPoint() {
new Path.Circle({
center: Point.random() * view.size,
radius: (1 * Math.random()) + 2.5,
fillColor: Color.random(),
parent: points
});
counter.content = +counter.content + 1;
}
function rasterize() {
// hide counter to avoid rasterizing it
counter.visible = false;
// rasterize the layer
const newRaster = project.activeLayer.rasterize();
// remove points (which are now useless)
points.removeChildren();
// remove previous raster
if (raster) {
raster.remove();
}
// store current raster
raster = newRaster;
// show counter
counter.visible = true;
}
function addStats() {
var script = document.createElement('script');
script.onload = function() {
var stats = new Stats();
document.body.appendChild(stats.dom);
requestAnimationFrame(function loop() {
stats.update();
requestAnimationFrame(loop);
});
};
document.head.appendChild(script);
}