Jumping Triangles

Developer
Size
2,350 Kb
Views
10,120

How do I make an jumping triangles?

What is a jumping triangles? How do you make a jumping triangles? This script and codes were developed by Anwesh Mishra on 08 January 2023, Sunday.

Jumping Triangles Previews

Jumping Triangles - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Jumping Triangles</title> <link rel="stylesheet" href="css/style.css">
</head>
<body> <canvas id="c1" width="1000px" height="600px">
</canvas> <script src="js/index.js"></script>
</body>
</html>

Jumping Triangles - Script Codes CSS Codes

#c1 { border:1px solid white;
}

Jumping Triangles - Script Codes JS Codes

var canvas = document.getElementById('c1')
var context = canvas.getContext('2d')
let setScreenSize = ()=>{ canvas.width = window.innerWidth/2 canvas.height = window.innerHeight context = canvas.getContext('2d')
}
setScreenSize()
window.onresize = () => { setScreenSize() initJumpingTriangles()
}
class JumpingTriangle { constructor(x,y,size,color) { this.x = x this.y = y this.deg = 0 this.color = color this.dir = 0 this.size = size } draw() { context.fillStyle=this.color context.save() context.translate(this.x,this.y) context.rotate(this.deg*Math.PI/180) context.beginPath() context.moveTo(-this.size/2,this.size/2) context.lineTo(this.size/2,this.size/2) context.lineTo(0,-this.size/2) context.fill() context.restore() } update() { this.y-=this.dir*(canvas.height/12) this.deg+=Math.abs(this.dir)*30 if(this.deg%180 == 0 && this.dir!=0) { this.dir *=-1; if(this.dir == 1) { this.dir = 0 } } } handleTap(x,y) { let condition = x>=this.x-this.size/2 && x<=this.x+this.size/2 && y>=this.y-this.size/2 && y<=this.y+this.size/2 if(condition) { this.dir = 1 } return condition }
}
var jumpingTriangles = []
var initJumpingTriangles = () => { jumpingTriangles = [] var colors = ["#FF6F00","#33691E","#FDD835","#880E4F","#B2FF59"] var gap = (2*canvas.width)/(3*colors.length+1),x = gap, y = canvas.height/2 colors.forEach((color)=>{ jumpingTriangles.push(new JumpingTriangle(x,y,gap,color)) x+=(gap*3)/2 })
}
initJumpingTriangles()
var time = 0
var bgColors = ["#009688","#03A9F4","#00BCD4","#E91E63"],index = 0
const draw = ()=>{ context.clearRect(0,0,canvas.width,canvas.height) context.fillStyle=bgColors[index] context.fillRect(0,0,canvas.width,canvas.height) if(time%20 == 0) { index++ index%=3 } jumpingTriangles.forEach((jumpingTriangle)=>{ jumpingTriangle.draw() jumpingTriangle.update() }) time++
}
canvas.onmousedown = (event)=>{ var x = event.pageX,y = event.pageY jumpingTriangles.forEach((jumpingTriangle)=>{ jumpingTriangle.handleTap(x,y) })
}
setInterval(draw,100)
Jumping Triangles - Script Codes
Jumping Triangles - Script Codes
Home Page Home
Developer Anwesh Mishra
Username Anwesh43
Uploaded January 08, 2023
Rating 3
Size 2,350 Kb
Views 10,120
Do you need developer help for Jumping Triangles?

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!

Anwesh Mishra (Anwesh43) Script Codes
Create amazing love letters 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!