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. 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

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

              
                ADD SAVE AS IMAGE FUNCTION

<br>

http://www.sitepoint.com/using-the-media-capture-api/?utm_source=html5weekly&utm_medium=email

<br>

<canvas class="c"></canvas>
<canvas class="d"></canvas>
<br>

<hr>

<h1>Features</h1>

<ul>
				<li>Function Chaining</li>
				<li>Set width, height, and color</li>
				<li>Draw rectangles</li>
				<li>Drawable Canvas</li>
				<li>Reset-able</li>
				<li>Draw Images</li>
				<li>Manipulate Event Listeners</li>
				<li>Clear shapes and lines</li>
	<li>Save and restore current state</li>
	<li>Much more coming!</li>
</ul>

https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D
              
            
!

CSS

              
                body {margin: 3em}
              
            
!

JS

              
                /* easyCanvas.js
*
* A JavaScript library to make working with the Canvas element easier
*
* Author: Tim Evko
* https://twitter.com/tevko
* https://timevko.com
* https://github.com/tevko/easyCanvas
* License: MIT
*
*/

/*eslint-env browser */

(function(window) {
    'use strict';
    var easyCanvas = window.easyCanvas = window.easyCanvas || {
        init: function(cls, ctx) {
            var cv = document.querySelector(cls),
                    ct = cv.getContext(ctx);
            this.canvas = cv;
            this.context = ct;
            this.width = window.getComputedStyle(cv).getPropertyValue('width');
            this.height = window.getComputedStyle(cv).getPropertyValue('height');
            this.bg = window.getComputedStyle(cv).getPropertyValue('background-color');
            this.shapesCount = 0;
            this.isDrawable = false;
            this.currentEventListenerCount = 0;
            this.imgCount = 0;
            return this;
        },
        setHeight: function(val) {
            this.height = val;
            this.canvas.style.height = val;
            return this;
        },
        setWidth: function(val) {
            this.width = val;
            this.canvas.style.width = val;
            return this;
        },
        setBG: function(color) {
            this.bg = color;
            this.canvas.style.backgroundColor = color;
            return this;
        },
        rectangle: function(cords, fill, lineWidth, strokeStyle) {
            this.context.fillStyle = fill;
            this.context.lineWidth = lineWidth;
            this.context.strokeStyle = strokeStyle;
            this.context.fillRect.apply(this.context, cords);
            this.context.strokeRect.apply(this.context, cords);
            this.currentShapes = this.currentShapes || {};
            this.currentShapes[this.shapesCount] = {
                'cords': cords,
                'fill': fill,
                'lineWidth': lineWidth,
                'strokeStyle': strokeStyle
            };
            this.shapesCount += 1;
            return this;
        },
        circle: function(centerX, centerY, radius, fillStyle, lineWidth, strokeStyle) {
            this.context.beginPath();
            this.context.arc(centerX, centerY, radius, 0, Math.PI * 2, true);
            this.context.fillStyle = fillStyle;
            this.context.fill();
            this.context.lineWidth = lineWidth;
            this.context.strokeStyle = strokeStyle;
            this.context.stroke();
            this.currentShapes = this.currentShapes || {};
            this.currentShapes[this.shapesCount] = {
                'centerX': centerX,
                'centerY': centerY,
                'radius': radius,
                'strokeStyle': strokeStyle,
                'fillStyle': fillStyle,
                'lineWidth': lineWidth
        };
            this.shapesCount += 1;
            return this;
        },
        makeDrawable: function(strokeStyle, lineJoin, lineWidth) {
            this.isDrawable = true;
            var clickX = [],
            clickY = [],
            clickDrag = [],
            paint,
            addClick = function(x, y, dragging) {
                clickX.push(x);
                clickY.push(y);
                clickDrag.push(dragging);
            },
            redraw = function() {
                this.context.clearRect(0, 0, this.context.canvas.width, this.context.canvas.height);
                this.context.strokeStyle = strokeStyle;
                this.context.lineJoin = lineJoin;
                this.context.lineWidth = lineWidth;
                for (var i = 0; i < clickX.length; i++) {
                    this.context.beginPath();
                    if (clickDrag[i] && i) {
                        this.context.moveTo(clickX[i - 1], clickY[i - 1]);
                    } else {
                        this.context.moveTo(clickX[i] - 1, clickY[i]);
                    }
                    this.context.lineTo(clickX[i], clickY[i]);
                    this.context.closePath();
                    this.context.stroke();
                }
            },
            msdn = function(e) {
                paint = true;
                addClick(e.clientX - this.offsetLeft, e.clientY - this.offsetTop);
            },
            msm = function(e) {
                if (paint) {
                    addClick(e.clientX - this.offsetLeft, e.clientY - this.offsetTop, true);
                    redraw();
                }
            },
            pfalse = function() {
                paint = false;
            };
            this.context.addEventListener('mousedown', msdn);
            this.context.addEventListener('mousemove', msm);
            this.context.addEventListener('mouseup', pfalse);
            this.context.addEventListener('mouseleave', pfalse);
            this.currentEventListeners = this.currentEventListeners || {};
            this.currentEventListeners[this.currentEventListenerCount] = {
                'mousedown': {'name': 'msdn', 'val': msdn},
                'mousemove': {'name': 'msm', 'val': msm},
                'mouseup': {'name': 'pfalse', 'val': pfalse},
                'mouseleave': {'name': 'pfalse', 'val': pfalse}
            };
            this.currentEventListenerCount += 1;
            return this;
        },
        drawImg: function(src, srcX, srcY, srcWidth, srcHeight, destX, destY, destWidth, destHeight) {
            if (typeof src === 'string') {
                var i = new Image();
                i.src = src;
            }
            var nsrc = i || src;
            this.context.drawImage(nsrc, srcX, srcY, srcWidth, srcHeight, destX, destY, destWidth, destHeight);
            this.currentImgs = this.currentImgs || {};
            this.currentImgs[this.imgCount] = {
                'source': nsrc,
                'srcX': srcX,
                'srcY': srcY,
                'srcWidth': srcWidth,
                'srcHeight': srcHeight,
                'destX': destX,
                'destY': destY,
                'destWidth': destWidth,
                'destHeight': destHeight
            };
            this.imgCount += 1;
            return this;
        },
        clearCanvas: function() {
            this.context.clearRect(0, 0, this.context.canvas.width, this.context.canvas.height);
            return this;
        },
        removeEvtListeners: function() {
            for (var evtl in this.currentEventListeners) {
                var eobj = this.currentEventListeners[evtl];
                for (var val in eobj) {
                    this.context.removeEventListener(val, eobj[val].val);
                }
            }
            this.currentEventListenerCount = 0;
            this.currentEventListeners = null;
            return this;
        },
        resetAll: function() {
            this.context.style.backgroundColor = 'transparent';
            this.context.style.width = 'auto';
            this.context.style.height = 'auto';
            this.removeEvtListeners();
            this.clearCanvas();
            this.currentEventListenerCount = 0;
            this.bg = 'transparent';
            this.currentShapes = null;
            this.height = 'auto';
            this.width = 'auto';
            this.isDrawable = false;
            this.shapesCount = 0;
            this.imgCount = 0;
            this.currentImgs = null;
            return this;
        },
        save: function() {
            this.context.save();
            return this;
        },
        restore: function() {
            this.context.restore();
            return this;
        }
    };
})(window);

var x = Object.create(easyCanvas);
x.init('.c', '2d').setBG('orange');
var y = Object.create(easyCanvas);
y.init('.d', '2d').setBG('green');
console.log(typeof x)
              
            
!
999px

Console