Last active
March 23, 2019 04:19
-
-
Save SabrinaMarkon/8a1fe7f0e295bfd2c6e6959c8c6207e4 to your computer and use it in GitHub Desktop.
Three.js Ball Bouncing off Walls - https://jsbin.com/rirahuw
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> | |
<title>Three.js Ball Bouncing off Walls</title> | |
<style id="jsbin-css"> | |
body { background-color:#ededed; font:normal 12px/18px Arial, Helvetica, sans-serif; } | |
h1 { display:block; width:600px; margin:20px auto; padding-bottom:20px; font:normal 24px/30px Georgia, "Times New Roman", Times, serif; color:#333; text-shadow: 1px 2px 3px #ccc; border-bottom:1px solid #cbcbcb; } | |
#container { width:600px; margin:0 auto; } | |
#myCanvas { background:#fff; border:1px solid #cbcbcb; } | |
</style> | |
</head> | |
<body> | |
<h1>Three.js Ball Bouncing off Walls</h1> | |
<div id="container"> | |
<canvas id="myCanvas" width="300" height="300"></canvas> | |
</div> | |
<script id="jsbin-source-html" type="text/html"> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> | |
<title>Three.js Ball Bouncing off Walls</title> | |
</head> | |
<body> | |
<h1>Three.js Ball Bouncing off Walls </h1> | |
<div id="container"> | |
<canvas id="myCanvas" width="300" height="300"></canvas> | |
</div> | |
</body> | |
</html> | |
<script type="text/javascript"> | |
var context; | |
var dx= 4; | |
var dy=4; | |
var y=150; | |
var x=10; | |
function draw(){ | |
context= myCanvas.getContext('2d'); | |
context.clearRect(0,0,300,300); | |
context.beginPath(); | |
context.arc(x,y,20,0,Math.PI*2,true); | |
context.closePath(); | |
context.fill(); | |
if( x<0 || x>300){ | |
context.fillStyle=getRandomColor(); | |
dx=-dx; | |
} | |
if( y<0 || y>300) | |
{ | |
context.fillStyle=getRandomColor(); | |
dy=-dy; | |
} | |
x+=dx; | |
y+=dy; | |
} | |
setInterval(draw,10); | |
function getRandomColor() { | |
var letters = '0123456789ABCDEF'; | |
var color = '#'; | |
for (var i = 0; i < 6; i++) { | |
color += letters[Math.floor(Math.random() * 16)]; | |
} | |
return color; | |
} | |
<\/script></script> | |
<script id="jsbin-source-css" type="text/css">body { background-color:#ededed; font:normal 12px/18px Arial, Helvetica, sans-serif; } | |
h1 { display:block; width:600px; margin:20px auto; padding-bottom:20px; font:normal 24px/30px Georgia, "Times New Roman", Times, serif; color:#333; text-shadow: 1px 2px 3px #ccc; border-bottom:1px solid #cbcbcb; } | |
#container { width:600px; margin:0 auto; } | |
#myCanvas { background:#fff; border:1px solid #cbcbcb; }</script> | |
</body> | |
</html> | |
<script type="text/javascript"> | |
var context; | |
var dx= 4; | |
var dy=4; | |
var y=150; | |
var x=10; | |
function draw(){ | |
context= myCanvas.getContext('2d'); | |
context.clearRect(0,0,300,300); | |
context.beginPath(); | |
context.arc(x,y,20,0,Math.PI*2,true); | |
context.closePath(); | |
context.fill(); | |
if( x<0 || x>300){ | |
context.fillStyle=getRandomColor(); | |
dx=-dx; | |
} | |
if( y<0 || y>300) | |
{ | |
context.fillStyle=getRandomColor(); | |
dy=-dy; | |
} | |
x+=dx; | |
y+=dy; | |
} | |
setInterval(draw,10); | |
function getRandomColor() { | |
var letters = '0123456789ABCDEF'; | |
var color = '#'; | |
for (var i = 0; i < 6; i++) { | |
color += letters[Math.floor(Math.random() * 16)]; | |
} | |
return color; | |
} | |
</script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
body { background-color:#ededed; font:normal 12px/18px Arial, Helvetica, sans-serif; } | |
h1 { display:block; width:600px; margin:20px auto; padding-bottom:20px; font:normal 24px/30px Georgia, "Times New Roman", Times, serif; color:#333; text-shadow: 1px 2px 3px #ccc; border-bottom:1px solid #cbcbcb; } | |
#container { width:600px; margin:0 auto; } | |
#myCanvas { background:#fff; border:1px solid #cbcbcb; } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment