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

              
                <div class="flex">
	<input type="button" id="edit" name="edit" value="はじめる" />
	<input type="button" id="eraser" name="eraser" value="消しゴム" />
	<input type="button" id="clear" name="clear" value="ぜんぶ消す" />
	<input type="button" id="end" name="end" value="おわり" />
</div>

<div id="canvasmenu" class="flex">
	<div class="icon-color bc-e91e63"></div>
	<div class="icon-color bc-ffeb3b"></div>
	<div class="icon-color bc-ff4801"></div>
	<div class="icon-color bc-536dfe"></div>
	<div class="icon-color bc-009688"></div>
	<div class="icon-color bc-00e676"></div>
	<div class="icon-color bc-555555"></div>
	<div class="icon-size icon-size-sm" data-size="5"><span>小</span></div>
	<div class="icon-size icon-size-md" data-size="10"><span>中</span></div>
	<div class="icon-size icon-size-lg" data-size="14"><span>大</span></div>
</div>

<div id="canvas-wrapper">
	<canvas id="my-canvas"></canvas>
</div>

<div class="flex">
	<div>X : </div>
	<div id="xpostion"></div>
</div>
<div class="flex">
	<div>Y : </div>
	<div id="ypostion"></div>
</div>
              
            
!

CSS

              
                	.flex {
		display: flex;
		flex-flow: row nowrap; /* default */
		justify-content: flex-start; /* default */
		margin-bottom: 10px;
	}

		.flex > input {
			margin-right: 10px;
		}

	.icon-color {
		width: 30px;
		height: 30px;
		border-radius: 50%;
		margin-right: 10px;
	}

	.bc-e91e63 {
		background-color: #e91e63;
	}

	.bc-ffeb3b {
		background-color: #ffeb3b;
	}

	.bc-ff4801 {
		background-color: #ff4801;
	}

	.bc-536dfe {
		background-color: #536dfe;
	}

	.bc-009688 {
		background-color: #009688;
	}

	.bc-00e676 {
		background-color: #00e676;
	}

	.bc-555555 {
		background-color: #555555;
	}

	.icon-size {
		width: 30px;
		height: 30px;
		border-radius: 50%;
		text-align: center;
		margin-right: 10px;
	}

		.icon-size span {
			vertical-align: middle;
		}

	.icon-size-sm {
		background-color: #555555;
		color: #ffffff;
	}

	.icon-size-md {
		background-color: #555555;
		color: #ffffff;
	}

	.icon-size-lg {
		background-color: #555555;
		color: #ffffff;
	}

	#canvas-wrapper {
		width: 640px;
		height: 380px;
		border: solid 1px black;
	}
              
            
!

JS

              
                class DrawingTools {

	constructor(canvasId) {
		this._canvas = document.getElementById(canvasId);
		this._canvas.width = this._canvas.parentNode.clientWidth;
		this._canvas.height = this._canvas.parentNode.clientHeight;
		this._ctx = this._canvas.getContext('2d');
		this._mouse = { x: null, y: null };
		this._defalutDraw = { size: 14, color: '#555555', alpha: 1.0, lineCap: 'round' };
		this._toolState = { initialized: false }
	}

	initialized() {

		const draw = (event) => {
			const x = event.layerX;
			const y = event.layerY;
			lineTo(x, y);
			document.getElementById("xpostion").textContent = x;
			document.getElementById("ypostion").textContent = y;
		}

		const lineTo = (x, y) => {
			this._ctx.globalAlpha = this._defalutDraw.alpha;
			this._ctx.lineCap = this._defalutDraw.lineCap;
			this._ctx.lineWidth = this._defalutDraw.size;
			this._ctx.strokeStyle = this._defalutDraw.color;

			if (this._mouse.x === null || this._mouse.y === null) {
				this._ctx.moveTo(x, y);
			} else {
				this._ctx.moveTo(this._mouse.x, this._mouse.y);
			}
			this._ctx.lineTo(x, y);
			this._ctx.stroke();
			this._mouse.x = x;
			this._mouse.y = y;
		}

		const closePath = () => {
			this._ctx.closePath();
			this._mouse.x = null;
			this._mouse.y = null;
		}

		const mousedownHandler = {
			handleEvent: (event) => {
				if (event.button === 0) {
					this._ctx.beginPath();
				}
			}
		};

		const mousemoveHandler = {
			handleEvent: (event) => {
				if (event.buttons === 1 || event.witch === 1) {
					draw(event);
				}
			}
		};

		const mouseupHandler = {
			handleEvent: (event) => {
				closePath();
			}
		};

		const mouseoutHandler = {
			handleEvent: (event) => {
				closePath();
			}
		};

		const startDrawingHandler = {
			handleEvent: () => {
				this._canvas.addEventListener('mousedown', mousedownHandler, { once: false, passive: true, capture: false });
				this._canvas.addEventListener('mousemove', mousemoveHandler, { once: false, passive: true, capture: false });
				this._canvas.addEventListener('mouseup', mouseupHandler, { once: false, passive: true, capture: false });
				this._canvas.addEventListener('mouseout', mouseoutHandler, { once: false, passive: true, capture: false });
			}
		};
		const edit = document.getElementById('edit');
		edit.addEventListener('click', startDrawingHandler, { once: false, passive: true, capture: false });

		const startEraserHandler = {
			handleEvent: () => {
				this._canvas.globalCompositeOperation = 'destination-out';
				this._defalutDraw.color = '#FFFFFF';
			}
		};
		const eraser = document.getElementById('eraser');
		eraser.addEventListener('click', startEraserHandler, { once: false, passive: true, capture: false });

		const clearDrawingHandler = {
			handleEvent: () => {
				this._ctx.beginPath();
				this._ctx.fillStyle = "#ffffff";
				this._ctx.globalAlpha = 1.0;
				this._ctx.fillRect(0, 0, this._canvas.width, this._canvas.height);
			}
		};
		const clear = document.getElementById('clear');
		clear.addEventListener('click', clearDrawingHandler, { once: false, passive: true, capture: false });

		const endDrawingHandler = {
			handleEvent: () => {
				this._canvas.removeEventListener('mousedown', mousedownHandler, { once: false, passive: true, capture: false });
				this._canvas.removeEventListener('mousemove', mousemoveHandler, { once: false, passive: true, capture: false });
				this._canvas.removeEventListener('mouseup', mouseupHandler, { once: false, passive: true, capture: false });
				this._canvas.removeEventListener('mouseout', mouseoutHandler, { once: false, passive: true, capture: false });
			}
		};
		const end = document.getElementById('end');
		end.addEventListener('click', endDrawingHandler, { once: false, passive: true, capture: false });

		const activateColorHandler = {
			handleEvent: (event) => {
				const style = window.getComputedStyle(event.target);
				const color = style.getPropertyValue('background-color');
				this._defalutDraw.color = color;
			}
		};
		const menus = document.getElementById('canvasmenu');
		const colorElems = menus.querySelectorAll('.icon-color');
		for (const elem of colorElems) {
			elem.addEventListener('click', activateColorHandler, { once: false, passive: true, capture: false });
		}

		const activateSizeHandler = {
			handleEvent: (event) => {
				const size = event.target.parentNode.getAttribute('data-size');
				this._defalutDraw.size = size;
			}
		};
		const sizeElems = menus.querySelectorAll('.icon-size');
		for (const elem of sizeElems) {
			elem.addEventListener('click', activateSizeHandler, { once: false, passive: true, capture: false });
		}

		this._toolState.initialized = true;
	}
}

window.addEventListener('load', () => {
	const tools = new DrawingTools('my-canvas');
	tools.initialized();
});
              
            
!
999px

Console