HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
Any URL's added here will be added as <link>
s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
Any URL's added here will be added as <script>
s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
Search for and use JavaScript packages from npm here. By selecting a package, an import
statement will be added to the top of the JavaScript editor for this package.
Using packages here is powered by Skypack, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ES6 import
usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<div id="ballsHolder">
<select id="dropBalls">
<option value="">Balls ??</option>
<option value="2">2</option>
<option value="5">5</option>
<option value="8">8</option>
<option value="10">10</option>
<option value="15">15</option>
<option value="20">20</option>
<option value="30">30</option>
<option value="40">40</option>
</select>
<canvas></canvas>
</div><!--end balls holder div -->
#ballsHolder {
position: relative;
overflow: hidden;
}
#dropBalls {
position: absolute;
top:0.5em;
left:0.5em;
}
// This is a modification of something I learned here:
//I added the drop list, https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Object_building_practice
// grabs the <canvas></canvas>, which is simply another html element
var canvas = document.querySelector('canvas');
var ctx = canvas.getContext('2d');//seems more like it should be setContext...?
//below is called chaining variables: chain = otherChain = anotherchain
var width = canvas.width = window.innerWidth;
var height = canvas.height = window.innerHeight;
var numberBalls = 10;
//this is select drop list
var dropBalls = document.getElementById('dropBalls');
dropBalls.addEventListener('change', addBallz);
function addBallz(){
numberBalls = dropBalls.value;
}
//function to generate random number
function randomMW(min,max) {
var num = Math.floor(Math.random()*(max-min)) + min;
return num;
}
//this is an object constructor, hence the Capital letter
function Ball(x, y, velX, velY, color, size) {
this.x = x;
this.y = y;
this.velX = velX;
this.velY = velY;
this.color = color;
this.size = size;
}
Ball.prototype.draw = function() {
ctx.beginPath();
ctx.fillStyle = this.color;
/*
The last two parameters (0, 2*PI) specify the start and end number
of degrees round the circle that the arc is drawn between.
Here we specify 0 degrees, and 2 * PI, which is the
equivalent of 360 degrees in radians
(annoyingly, you have to specify this in radians).
That gives us a complete circle.
If you had specified only 1 * PI, you'd get a semi-circle (180 degrees).
*/
ctx.arc(this.x, this.y, this.size, 0, 2 * Math.PI);
/*
basically states "finish drawing the path we started with
beginPath(), and fill the area it takes up with
the color we specified earlier in fillStyle."
*/
ctx.fill();//
}
Ball.prototype.update = function(){
//makes balls 'bounce' off wall
if ((this.x + this.size) >= width) {
this.velX = -(this.velX);
}
if ((this.x - this.size) <= 0) {
this.velX = -(this.velX);
}
if ((this.y + this.size) >= height) {
this.velY = -(this.velY);
}
if ((this.y - this.size) <= 0) {
this.velY = -(this.velY);
}
this.x += this.velX;
this.y += this.velY;
}//end ball.update
Ball.prototype.collisionDetect = function(){
for(var j = 0; j < balls.length; j++){
/*is the ball we are checking not the current one
in the loop, if so, proceed.
Put another way: is the current ball
(whose collisionDetect is being invoked)
the same as the loop ball?
Then we negate the check ( ! ) so the statement only
runs if the are *not* the same
*/
if(!(this === balls[j])){
/*check to see if two circles overlap - collide
*/
var dx = this.x - balls[j].x;
var dy = this.y - balls[j].y;
var distance = Math.sqrt(dx * dx + dy * dy);
//if a collision was detected
if(distance < this.size + balls[j].size){
balls[j].color = this.color = 'rgb(' + randomMW(0,255) + ',' + randomMW(0,255) + ',' + randomMW(0,255) + ')';
}//end if(distance....)
}// end if(!(this))
}//end for (var j)
}
var balls = [];// we add array values via balls.push(ball);
function loop(){
ctx.fillStyle = 'rgba(0, 0, 0, 0.25)';
ctx.fillRect(0, 0, width, height);
if (balls.length < numberBalls) {
var ball = new Ball (
randomMW(0,width),
randomMW(0,height),
randomMW(-5,5),//-7,7
randomMW(-5,5),//this is velocity
'rgb(' + randomMW(0,255) + ',' + randomMW(0,255) + ',' + randomMW(0,255) + ')',
randomMW(10,20)
);
balls.push(ball);//'push(es) a ball instance into balls[]'
} else if (balls.length > numberBalls) {
balls.pop(ball);
}//end if balls.length
for (var i = 0; i < balls.length; i++) {
balls[i].draw();
balls[i].update();
balls[i].collisionDetect();
}
requestAnimationFrame(loop);//runs frames continously set speed per second
}//end function loop()
loop();
Also see: Tab Triggers