Pixi JS

Developer
Size
2,710 Kb
Views
58,696

How do I make an pixi js?

What is a pixi js? How do you make a pixi js? This script and codes were developed by Tim on 31 October 2022, Monday.

Pixi JS Previews

Pixi JS - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Pixi JS</title>
</head>
<body> <script src='https://cdnjs.cloudflare.com/ajax/libs/pixi.js/4.0.3/pixi.min.js'></script> <script src="js/index.js"></script>
</body>
</html>

Pixi JS - Script Codes JS Codes

function contain(sprite, container) { var collision = undefined; //Left if (sprite.x < container.x) { sprite.x = container.x; collision = "left"; } //Top if (sprite.y < container.y) { sprite.y = container.y; collision = "top"; } //Right if (sprite.x + sprite.width > container.width) { sprite.x = container.width - sprite.width; collision = "right"; } //Bottom if (sprite.y + sprite.height > container.height) { sprite.y = container.height - sprite.height; collision = "bottom"; } //Return the `collision` value return collision;
}
function hitTestRectangle(r1, r2) { //Define the variables we'll need to calculate var hit, combinedHalfWidths, combinedHalfHeights, vx, vy; //hit will determine whether there's a collision hit = false; //Find the center points of each sprite r1.centerX = r1.x + r1.width / 2; r1.centerY = r1.y + r1.height / 2; r2.centerX = r2.x + r2.width / 2; r2.centerY = r2.y + r2.height / 2; //Find the half-widths and half-heights of each sprite r1.halfWidth = r1.width / 2; r1.halfHeight = r1.height / 2; r2.halfWidth = r2.width / 2; r2.halfHeight = r2.height / 2; //Calculate the distance vector between the sprites vx = r1.centerX - r2.centerX; vy = r1.centerY - r2.centerY; //Figure out the combined half-widths and half-heights combinedHalfWidths = r1.halfWidth + r2.halfWidth; combinedHalfHeights = r1.halfHeight + r2.halfHeight; //Check for a collision on the x axis if (Math.abs(vx) < combinedHalfWidths) { //A collision might be occuring. Check for a collision on the y axis if (Math.abs(vy) < combinedHalfHeights) { //There's definitely a collision happening hit = true; } else { //There's no collision on the y axis hit = false; } } else { //There's no collision on the x axis hit = false; } //`hit` will be either `true` or `false` return hit;
};
var renderer = PIXI.autoDetectRenderer(256,256, {backgroundColor: 0xeeeeee, antialias: true})
document.body.appendChild(renderer.view)
var stage = new PIXI.Container()
var hitEvent = false
var rectangle = null, circle = null
setup()
function setup(){ rectangle = new PIXI.Graphics() rectangle.beginFill(0x709FFC) rectangle.drawRect(0,0,32,32) rectangle.vx = 2 rectangle.vy = 3 rectangle.endFill() circle = new PIXI.Graphics() circle.beginFill(0x66CCFF) circle.drawCircle(0,0,16) circle.x = 0 circle.y = 100 circle.vx = 3 circle.vy = 1 circle.endFill() console.log(circle) stage.addChild(rectangle) stage.addChild(circle) animate()
}
function keepOnScreen(element, shape){ let circleContainer = {x: element.halfWidth, y:element.halfWidth, width: 256+element.halfWidth, height: 256+element.halfWidth} let rectContainer = {x: 0, y:0, width: 256, height: 256} let border = (shape === 'circle') ? circleContainer : rectContainer //Check if the rectangle hits the borders var hit = contain(element, border) //If there is a collision then change the movement direction if(hit === 'right' || hit === 'left'){ element.vx *= -1 } if(hit === 'top' || hit === 'bottom'){ element.vy *= -1 }
}
function animate(){ let testHit = hitTestRectangle(rectangle,circle) if(hitEvent === false && testHit === true){ rectangle.clear() rectangle.beginFill( PIXI.utils.rgb2hex([ Math.floor(Math.random()*255), Math.floor(Math.random()*255), Math.floor(Math.random()*255) ]) ) rectangle.drawRect(0,0,32,32) rectangle.endFill() circle.clear() circle.beginFill( PIXI.utils.rgb2hex([ Math.floor(Math.random()*255), Math.floor(Math.random()*255), Math.floor(Math.random()*255) ]) ) circle.drawCircle(0,0,16) circle.endFill() } hitEvent = testHit keepOnScreen(rectangle) keepOnScreen(circle, 'circle') //Update the position rectangle.x += rectangle.vx rectangle.y += rectangle.vy circle.x += circle.vx circle.y += circle.vy //render the stage renderer.render(stage) requestAnimationFrame(animate)
}
Pixi JS - Script Codes
Pixi JS - Script Codes
Home Page Home
Developer Tim
Username maytim
Uploaded October 31, 2022
Rating 3
Size 2,710 Kb
Views 58,696
Do you need developer help for Pixi JS?

Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!

Tim (maytim) Script Codes
Create amazing captions with AI!

Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!