Pen Settings

HTML

CSS

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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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

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.

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 width="120px" height="100px"></canvas>

<p>Canvas <span id="svg_state">is not</span> being interacted with.</p>

<p>Cloud edge <span id="cloud_state">is not</span> being interacted with</p>

<p>Mouse coordinates: <span id="mouse_coords"></span></p>
              
            
!

CSS

              
                canvas {
	width: 50%;
	max-width: 200px;
	border: 1px dashed black;
}
              
            
!

JS

              
                //  @ts-check

const canvasStateDisplay = document.querySelector('#svg_state');
const cloudStateDisplay = document.querySelector('#cloud_state');
const mouseCoordsDisplay = document.querySelector('#mouse_coords');
let canvasInteracting = false;
let cloudInteracting = false;
let translatedMouseCoords = { x: 0, y: 0 };

const cloudSvg = document.querySelector('svg');
const canvas = document.querySelector('canvas');
const ctx = canvas.getContext('2d');

/**
 * For a given mouse coordinate, translate to corresponding coordinate within original canvas element
 * @param mouseEvent {{clientX: number, clientY: number}}
 * @param canvasElem {HTMLCanvasElement}
 */
function getCanvasCoords(mouseEvent, canvasElem) {
	const canvasBoundingRect = canvasElem.getBoundingClientRect();
	const scale = {
		x: canvasElem.width / canvasBoundingRect.width,
		y: canvasElem.height / canvasBoundingRect.height,
	};
	return {
		x: (mouseEvent.clientX - canvasBoundingRect.left) * scale.x,
		y: (mouseEvent.clientY - canvasBoundingRect.top) * scale.y,
	};
}

function updateDisplay() {
	canvasStateDisplay.textContent = canvasInteracting ? 'is' : 'is not';
	cloudStateDisplay.textContent = cloudInteracting ? 'is' : 'is not';
	mouseCoordsDisplay.textContent = JSON.stringify(translatedMouseCoords);
}

// Insert cloud into canvas
const cloudSvgPath = `m31.93768,83.56317a24.64828,23.57661 0 0 1 0,-48.2249a26.79161,24.11245 0 0 1 58.94155,10.71664l5.35832,0a18.75413,18.75413 0 0 1 0,37.50826l-64.29987,0`;
const cloudPath = new Path2D(cloudSvgPath);
ctx.lineWidth = 3;
ctx.strokeStyle = 'black';
ctx.stroke(cloudPath);

/**
 * @param evt {MouseEvent}
 */
function hitDetect(evt) {
	// Canvas might be offset and/or scaled, so have to translate mouse position to *original* canvas coordinates
	translatedMouseCoords = getCanvasCoords(evt, canvas);
	// Temporarily inflate path for bigger hit-box for mouse
	const originalLineWidth = ctx.lineWidth;
	ctx.lineWidth = 16;
	cloudInteracting = ctx.isPointInStroke(cloudPath, translatedMouseCoords.x, translatedMouseCoords.y);
	// Reset path width
	ctx.lineWidth = originalLineWidth;
	updateDisplay();
}

// Attach mouse listener to entire canvas
canvas.addEventListener('mousemove', (evt) => {
	canvasInteracting = true;
	hitDetect(evt);
});
canvas.addEventListener('mouseleave', (evt) => {
	canvasInteracting = false;
	cloudInteracting = false;
	updateDisplay();
});

              
            
!
999px

Console