JSFiddle - React, Tailwind, and code Playground
HTML
<div id="stage"></div>
JavaScript
function movieCode() {
//********* demo logic ***********//
function mainLogic(){
new Rect(25,25,250,250,5).fill('yellow').addTo(stage);
// create a new physics engine
var physics = new Physics();
// create a new item and add it to the physics engine
function add(x,y,course,speed){
var group = new Group().attr({x:x,y:y}).addTo(stage);
new Circle(20,20,20).fill('random').addTo(group);
var item = new Item(group, x, y, 20, 1);
physics.addItem(item);
physics.push(item, course, speed, 0);
physics.moving = true;
}
// shoot a circle to the board
function go(){
var x = (Math.round(Math.random())*250) + 25;
var y = (Math.round(Math.random())*250) + 25;
var course = Math.random() * (Math.PI/2);
if (x > 25) course += Math.PI/2;
if (y > 25) course = -course;
add(x,y,course,20);
}
// create one circle every second, shoot it to the board
setInterval(go, 1000);
// cleanup circles. start cleaning up after 10 seconds (10 items).
// clean the oldest at every call.
setTimeout(function(){
setInterval(function(){
var x = physics.items.shift().bs.animate('1s',{opacity:0},{onEnd:function(){ x=x.destroy(); }});
}, 1000);
}, 10000);
// on tick, update position of all items
stage.on('tick', function(){
physics.tick();
physics.items.forEach(function(item){
item.bs.attr({x:item.pos.x, y:item.pos.y});
});
});
}
//**** physics.js ******//
// This class refers to the Item class, which is an abstract
// class with only a few properties to do math with.
var Physics = (function(){
// Some definitions:
// - course: current direction of movement
// - bearing: current direction the top of the item is facing
var config = {
stepCollisions: false,
maxSpeed: 40,
record_shots: false,
drawCourse: false,
drawCollisionAngles: false,
...