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

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

              
                <body onload="init()">
    <div id="toolbar">
        <div id="functions">
            <span id="tools">
                <button id="brush" class="penciltool">P</button>
                <button id="eraser" class="erasertool">E</button>
                <div id="save" class="savetool">S</div>
            </span>
        </div>
    </div>
    
    <div id="content">
      <canvas id="sketch"></canvas>
    </div>
  
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    
</body>
              
            
!

CSS

              
                html, body {
    margin:0px;
	  padding:0px;
    width:100%; 
    height:100%; 
}

#sketch {
    float:left;
    position:relative;
    cursor: crosshair;
}

#canvasInput {
  display: none;
}

#toolbar{
    float: left;
    width: 40px;
    height: 100%;
    padding: 7px;
    position: fixed;
    background-color: #2f2f2f;
    z-index: 1;
}

.penciltool{
	margin: 2px;
	width: 30px;
	height: 30px;
	cursor: pointer;
	background-color: #00b6ff;
}

.erasertool{
	margin: 2px;
	width: 30px;
	height: 30px;
	cursor: pointer;
	background-color: white;
}

.savetool{
	margin: 2px;
	width: 30px;
	height: 30px;
	cursor: pointer;
	background-color: grey;
}
              
            
!

JS

              
                var canvas, ctx;
var mouseX,mouseY,mouseDown=0;
var lastX,lastY=-1;

var tool = 'brush';

$('#tools button').on('click touch', function(){
	tool = $(this).attr('id');
	console.log(tool);
})

function drawLine(ctx,x,y,size) {
    if (lastX==-1) {
        lastX=x;
        lastY=y;
    }
    ctx.strokeStyle = '#00b6ff';
    ctx.fillStyle = '#00b6ff';
    ctx.lineCap = "round";
    ctx.beginPath();
    ctx.moveTo(lastX,lastY);
    ctx.lineTo(x,y);
    ctx.lineWidth = size;
    ctx.stroke();
    ctx.closePath();
    
    lastX=x;
    lastY=y;
}

function drawEraser(ctx,x,y,size) {
    if (lastX==-1) {
        lastX=x;
        lastY=y;
    }
    ctx.strokeStyle = '#ffffff';
    ctx.fillStyle = '#ffffff';
    ctx.lineCap = "round";
    ctx.beginPath();
    ctx.moveTo(lastX,lastY);
    ctx.lineTo(x,y);
    ctx.lineWidth = 5;
    ctx.stroke();
    ctx.closePath();
    
    lastX=x;
    lastY=y;
}

// download canvas
var callDownload = function() {
		download(sketch,'apontamento.png');
		};
	
	document.getElementById("save").addEventListener("click", callDownload);
	
	function download(canvas, filename) {

	//create a dummy CANVAS

    // create an "off-screen" anchor tag
    var lnk = document.createElement('a'),
        e;

    // the key here is to set the download attribute of the a tag
    lnk.download = filename;

    // convert canvas content to data-uri for link. When download
    // attribute is set the content pointed to by link will be
    // pushed as "download" in HTML5 capable browsers
    lnk.href = canvas.toDataURL();

    // create a "fake" click-event to trigger the download
    if (document.createEvent) {

        e = document.createEvent("MouseEvents");
        e.initMouseEvent("click", true, true, window,
                         0, 0, 0, 0, 0, false, false, false,
                         false, 0, null);

        lnk.dispatchEvent(e);

    } else if (lnk.fireEvent) {

        lnk.fireEvent("onclick");
    }
};


function sketch_mouseDown() {
    mouseDown=1;
    onTool();
}

function sketch_mouseUp() {
    mouseDown=0;
    lastX=-1;
    lastY=-1;
}

function sketch_mouseMove(e) { 
    getMousePos(e);
    if (mouseDown==1) {
        onTool();
    }
}

function getMousePos(e) {
    if (!e)
        var e = event;

    if (e.offsetX) {
        mouseX = e.offsetX;
        mouseY = e.offsetY;
    }
    else if (e.layerX) {
        mouseX = e.layerX;
        mouseY = e.layerY;
    }
}

function init() {
    canvas = document.getElementById('sketch');
    canvas.width = window.innerWidth;
    canvas.height = window.innerHeight;
  
    if (canvas.getContext)
        ctx = canvas.getContext('2d');

    if (ctx) {
        canvas.addEventListener('mousedown', sketch_mouseDown, false);
        canvas.addEventListener('mousemove', sketch_mouseMove, false);
        window.addEventListener('mouseup', sketch_mouseUp, false);
    }

    var img = new Image();
    img.onload = function() {
        ctx.drawImage(img, 30, 0);
    };
    img.src = 'https://cdn.sstatic.net/stackexchange/img/logos/so/so-icon.png';
}

var onTool = function() {
	
	if ( tool == 'brush' )
	{	drawLine(ctx,mouseX,mouseY,2); }
	
	else if ( tool == 'eraser' )
	{	drawEraser(ctx,mouseX,mouseY,2); }
  
};
              
            
!
999px

Console