cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

Looking for quick-add? Try the external resource search, it's quicker and gives you access to the most recent version of thousands of libraries. ☝️

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

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.

+ add another resource

Looking for quick-add? Try the external resource search, it's quicker and gives you access to the most recent version of thousands of libraries. ☝️

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <div id="output"></div>
<div class="button" id="fruit">Fruit Please</div>
<canvas width="400" height="300" id="drawer"></canvas>
<div class="button" id="newball">Add Ball</div>
            
          
!
            
              .button {
    width: 200px;
    height: 40px;
    background: #111111;
    color: #ffffff;
    padding-top: 20px;
    text-align: center;
    font-family: Tahoma, sans-serif;
    margin-left: auto;
    margin-right: auto;
}

.button:hover {
    background: #aaaaaa;
    cursor: pointer;
}
            
          
!
            
              //Intitialise Global Variables
var fruit = [], canvas, circles = [];

//Store canvas as variable for easy access
canvas = document.getElementById("drawer");

//Populate our fruit array
fruit[0] = "Mandarin";
fruit[1] = "Pineapple";
fruit[2] = "Orange";
fruit[3] = "Apple";

//Function that outputs a random fruit from our array
//Note the floor function: rounds the number down to the nearest whole number
function getFruit() {
    document.getElementById("output").innerHTML = fruit[Math.floor(Math.random() * fruit.length)];
}

//Function that redraws the circles on the canvas and then waits 10ms before calling the function again
function redraw() {
    //Get the canvas context
    var ctx = canvas.getContext("2d"), circle, ind;
    
    //Clear the canvas of any previous drawing
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    
    //Loop through all of our circles
    for (ind in circles) {
        //Store circle into temporary variable
        circle = circles[ind];
        
        //Add the speed values of the circle to its position
        circle.x += circle.hSpeed;
        circle.y += circle.vSpeed;
        
        //If the circle has gone over the bounds of the canvas, bounce!
        if ((circle.x < 0) || (circle.x > canvas.width)) {
            circle.hSpeed = -circle.hSpeed;
        }
        
        if ((circle.y < 0) || (circle.y > canvas.height)) {
            circle.vSpeed = -circle.vSpeed;
        }
        
        //Start drawing
        ctx.beginPath();
        
        //Draw an arc. Arguments are x (pixels), y (pixels), radius (pixels), startPoint (radians), endPoint (radians)
        ctx.arc(circle.x, circle.y, 5, 0, Math.PI * 2);
        
        //Fill our arc/circle!
        ctx.fill();
    }
    
    //Wait 10ms before calling the redraw function again
    setTimeout(redraw, 10);
}

//Only call redraw once as it will handle the rest
redraw();

//Adds a ball to our array and randomly sets some parts of it
function addBall() {
    //Create the object
    var circle = {};
    //Set its coordinates as random numbers from 0 - canvas bounds
    //x = 0; y = 0; is the top left corner of the screen
    //As x gets higher, the position goes to the right. As y gets higher, the position goes down
    circle.x = Math.random() * canvas.width;
    circle.y = Math.random() * canvas.height;
    
    //Sets random speed values which we will access later, between -1 and 1
    circle.hSpeed = Math.random() * 2 - 1;
    circle.vSpeed = Math.random() * 2 - 1;
    
    //Adds the circle object to our circle array for drawing under the function
    circles.push(circle);
}

//Binds our functions to their respective buttons
document.getElementById("fruit").onclick = getFruit;
document.getElementById("newball").onclick = addBall;
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console