Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs 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 its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

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.

Behavior

Auto Save

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <canvas id="kale"></canvas>
<div id="controls">
    <h3>KALEIDOSCOPE</h3>
    <div id="slider-holder">
        <span class="tag">less</span>
        <input id="slider-1" class="slider" type="range" step="2" value="12" max="14" min="6">
        <span class="tag">more</span>
    </div>     
</div>
<img id="defaultImg" class="hidden" src="https://images2.imgbox.com/2a/f0/cgDoYFC1_o.jpg"></img>
              
            
!

CSS

              
                * {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}
html {
    font-family: sans-serif;
}
body {
    background-color: #aaa;
    color: #fff;
    overflow: hidden;
}
#controls {
    padding: 0.5rem;
    position: absolute;
    top: 0;
    width: 100%;
    background-color: #888;
    background-color: rgba(0, 0, 0, 0.35);
}
#controls h3 {
    display: inline-block
}
#slider-holder {
    float: right;
}
.tag {
    padding: 0 0.5rem
}
.hidden {
    display: none;
}
/* range input formatting
 * relies heavly on this:
 * https://codepen.io/thebabydino/pen/pvpJWX
 */
input[type=range] {
    -webkit-appearance: none;
    -moz-appearance: none;
    background-color: transparent;
    display: inline-block;
    margin: 0 auto;
    width: 11rem;
    height: 1rem;
    cursor: pointer;
}
/* thumb */
input[type='range']::-webkit-slider-thumb {
    -webkit-appearance: none;
    position: relative;
    margin: -0.25em;
    border: none;
    /* Firefox, IE */
    width: 1em;
    height: 1em;
    border-radius: 0.5em;
    background: #fff;
}
input[type='range']::-moz-range-thumb {
    border: none;
    /* Firefox, IE */
    width: 1em;
    height: 1em;
    border-radius: 0.5em;
    cursor: ew-resize;
    background: #fff;
}
input[type='range']::-ms-thumb {
    border: none;
    /* Firefox, IE */
    width: 1em;
    height: 1em;
    border-radius: 0.5em;
    background: #fff;
}
/* track */
input[type="range"]::-webkit-slider-runnable-track {
    -webkit-appearance: none;
    height: 0.5em;
    border-radius: 0.25em;
    background: #777;
}
input[type='range']::-moz-range-track {
    border: none;
    height: 0.5em;
    border-radius: 0.25em;
    background: #777;
}
input[type='range']::-ms-track {
    border: none;
    height: 0.5em;
    border-radius: 0.25em;
    background: #777;
    color: transparent;
}
              
            
!

JS

              
                //setup global variables
var canvas = document.getElementById("kale");
canvas.height = window.innerHeight;
canvas.width = window.innerWidth;

(function draw() {
        var ctx = canvas.getContext('2d');
        var img = new Image();
        var slider = document.getElementById("slider-1");
        var triangles = slider.value; //amount of triangles for kaleidoscope
            
        img.onload = function(){
            //getting image dimensions so entire canvas width is covered
            var diagonal = Math.sqrt(Math.pow(canvas.width,2) + Math.pow(canvas.height,2));
            var scaleFactor = img.width/img.height;
            var h = diagonal/2;
            var w = parseInt(scaleFactor*h);
            
            //event listeners
            //draw kaleidoscope once before mouse movement detection begins
            kaleidoscope(w, h, img, canvas, ctx, triangles, 0);
            
            //draw kaleidoscope when mouse enters window
            canvas.addEventListener('mouseenter', function(e){
                var mouseX = e.clientX.map(0, canvas.width, -w/10, w/10);
                window.requestAnimationFrame(function(){kaleidoscope(w, h, img, canvas, ctx, triangles, mouseX);});
            });
            
            //redraw kaleidoscope when mouse moves along x-axis
            canvas.addEventListener('mousemove', function(e){
                var mouseX = e.clientX.map(0, canvas.width, -w/8, w/8);
                window.requestAnimationFrame(function(){kaleidoscope(w, h, img, canvas, ctx, triangles, mouseX);});
            });
            
            //redraw kaleidoscope when number of triangles is vhanged by input range
            slider.addEventListener("input", function(e){
             triangles = slider.value;
                window.requestAnimationFrame(function(){kaleidoscope(w, h, img, canvas, ctx, triangles, 0);});
            });
            
            //redraw kaleidoscope when window size changes
            window.addEventListener("resize", function(){
                canvas.height = window.innerHeight;
                canvas.width = window.innerWidth;
                window.requestAnimationFrame(function(){kaleidoscope(w, h, img, canvas, ctx, triangles, 0);});
                diagonal = Math.sqrt(Math.pow(canvas.width,2) + Math.pow(canvas.height,2));
                scaleFactor = img.width/img.height;
                h = diagonal/2;
                w = parseInt(scaleFactor*h);
            });
            
        }
    img.src = document.getElementById("defaultImg").src;
})();

/* draws a kaleidoscope
 * tranlates coordinates to center of canvas and then rotates by vertexAngle
 * then sets up the triangle clipping mask 
 * and rotates again by baseAngle so the image is perpendicular to the vertexAngle
 * finally draws the image (as is, or mirrored)
 */
function kaleidoscope(w, h, img, canvas, ctx, triangles, offset){
    //creates the triangle for the clipping mask
    var triangleClip = new Path2D();
    triangleClip.moveTo(0,0);
    triangleClip.lineTo(canvas.width, 0);
    triangleClip.lineTo(canvas.width*Math.cos((Math.PI/180)*(360/triangles)),canvas.width*Math.sin((Math.PI/180)*(360/triangles)));
    triangleClip.lineTo(0, 0);
    
    //gets the angles of the triangleClip
    var vertexAngle = 360/triangles;
    var baseAngle = (180 - vertexAngle)/2;
    
    //start drawing triangles
    for (var i=0; i < triangles; i++){
        ctx.save();
        ctx.translate(canvas.width/2, canvas.height/2);
        ctx.rotate((Math.PI/180)*vertexAngle*i);
        ctx.clip(triangleClip);
        if (i % 2 === 0) {
            ctx.rotate(-(Math.PI/180)*baseAngle);
            mirror(img, ctx, w/2+offset, 0, w, h);
        }
        else {
            ctx.rotate(-(Math.PI/180)*baseAngle);
            ctx.drawImage(img, -w/2+offset, 0, w, h);
        }
        ctx.restore();
    }
}

//http://stackoverflow.com/questions/8168217/html-canvas-how-to-draw-a-flipped-mirrored-image
function mirror(v, canvas, x, y, width, height){
    canvas.save();
    canvas.scale(-1,1);
    canvas.drawImage(v, x ,y ,width*-1,height);
    canvas.restore();
}

//http://stackoverflow.com/questions/10756313/javascript-jquery-map-a-range-of-numbers-to-another-range-of-numbers#23202637
/* maps number from one range to another */
Number.prototype.map = function (in_min, in_max, out_min, out_max) {
  return (this - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
              
            
!
999px

Console