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


Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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


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.

Editor Settings

Code Indentation

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

Visit your global Editor Settings.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.


              <canvas id="canvas" width="400px" height="250px"></canvas>




              	var canvas = document.getElementById("canvas");
		var context = canvas.getContext("2d");
		// canvas的宽度
		var width = 400;
		// canvas的高度
		var height = 250;

		function init() { 
			// 先在canvas上画一个灰色的矩形
			context.fillStyle = '#ddd';
			context.fillRect(0, 0, width, height); 
			// 设置canvas的背景为一张图片
			// 因为 getImageData 方法需要跨域,所以这里用 base64来保存图片 = 'url("") no-repeat center'; = "100% 100%";
			// 设置画的线的宽度			
			context.lineWidth = 35;
			// 设置线交汇时,是圆角的
			context.lineJoin = "round";


		canvas.addEventListener('mousedown', mouseDown, false);
		canvas.addEventListener('mousemove', mouseMove, false);
		canvas.addEventListener('mouseup', mouseUp, false);
		/* 如果需要移动端也可以生效,
		需要绑定touchstart、touchmove、touchend 事件,并且获取触摸点的坐标
		//canvas.addEventListener('touchstart', mouseDown, false);
		//canvas.addEventListener('touchmove', mouseMove, false);
		//canvas.addEventListener('touchend', mouseUp, false);

		// 判断是否可以画线
		var isDrawing; 
		// 保存开始画线时,线的起点的X坐标
		var startX=0;
		// 保存开始画线时,线的起点的Y坐标
		var startY=0; 
		// removeGrey 用来判断是否擦除全部灰色
		var removeGrey=0;
		// 按下鼠标按钮时,调用mouseDown
		function mouseDown(e){
			 isDrawing = true;
			 // 保存鼠标点击时 X坐标为,画线时,线的起点的X坐标
			 startX = e.layerX;
			 // 保存鼠标点击时 Y坐标为,画线时,线的起点的Y坐标
			 startY = e.layerY;

			 /* 移动端使用下面的方法 获取 startX 和 startY
			 startX = e.touches[0].clientX;
			 startY = e.touches[0].clientY;

		// 鼠标移动时,调用mouseDown
		function mouseMove(e){
			// 获取鼠标相对于 canvas 的坐标
			var x = e.layerX;
			var y = e.layerY;

			/* 移动端使用下面的方法 获取 x 和 y
			var x = e.touches[0].clientX;
			var y = e.touches[0].clientY;
			context.globalCompositeOperation = "destination-out"; 

			// 开始画线
			// 起点坐标为 startX 和 startY
			context.moveTo(startX, startY);
			// 结束的坐标为这次移动时的坐标
			context.lineTo(x, y);

			// 设置这次移动结束时的坐标,为下次开始画线时的坐标
			startX = x;
			startY = y; 

		// 松开鼠标按钮时,调用的事件
		function mouseUp(e){
			// isDrawing 为false时,不可以画线
			isDrawing = false;
			// 当removeGrey > 100时,清除全部灰色
				 context.fillStyle = "blue";
				 context.fillRect(0, 0, width, height);