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.

Quick-add: + add another resource

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.

Quick-add: + add another resource

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.

            
              <head>
	<title>Cantor Set</title>
</head>
<body>
	<canvas></canvas>
	<script src="cantor.js" type="text/javascript"></script>
</body>

            
          
!
            
              body {
		width: 100%;
		height: 100vh;
		margin: 0;
		padding: 0;
	}

	canvas {
	    margin: auto;
	}
            
          
!
            
              "use strict";

var canvas = document.querySelector('canvas');
var context = canvas.getContext("2d");

const pixelRatio = Math.round(window.devicePixelRatio) || 1; // used to render high res image on retina screens
const barHeight = 30;
const level = 8;

function fitToContainer() {
    canvas.style.width  = '100%';
    canvas.width        = canvas.offsetWidth * pixelRatio;
    canvas.style.height = barHeight * 2 * level / pixelRatio + 'px';
    canvas.height       = canvas.offsetHeight * pixelRatio;

    context.strokeStyle = "#000";
    context.lineWidth = barHeight;

    drawCantor(level, level, 0, canvas.width);
}

fitToContainer();
window.addEventListener('resize', fitToContainer);

function drawCantor(level, max_level, x_start, x_end) {
    // only generate levels 1 - 13; hard to see levels beyond 13
    if (level < 1 || level > 13) return;

    // draw level
    context.beginPath();
    var y = barHeight/2 + (max_level - level) * barHeight * 2;
    context.moveTo(x_start, y);
    context.lineTo(x_end, y);
    context.strokeStyle = "hsl(" + (360 - 120*level/max_level) + ",100%,30%)";
    context.stroke();
    context.closePath();

    drawCantor(level - 1, max_level, x_start,  x_start + (x_end - x_start)/3); // left
    drawCantor(level - 1, max_level, x_start + 2/3*(x_end - x_start), x_end);  // right
}
            
          
!
999px
Loading ..................

Console