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 id="canvassample" width="500" height="300"></canvas>
</div>
<div style="padding:10px">
    <button type="button" onclick="clearCanvas()">リセット</button>
  <button type="button" onclick="prevCanvas()">戻る</button>
  <button type="button" onclick="nextCanvas()">進む</button>
</div>
<div style="padding:10px">
    <button type="button" onclick="chgImg()" value="1">画像変換</button>
<h2>画像出力<h2>  
<div id="img-box"><img id="newImg"></div>

              
            
!

CSS

              
                canvas {
	position: relative;
	border:3px solid #000;
}

div#img-box{
  border:3px solid #000;
  width:500px;
}

div#btn-box{
	position: fixed;
	bottom :0px;
}


              
            
!

JS

              
                var canvas = document.getElementById('canvassample'),
    ctx = canvas.getContext('2d'),
    moveflg = 0,
    Xpoint,
    Ypoint,
    temp = [];

//初期値(サイズ、色、アルファ値)の決定
var defSize = 7,
    defColor = "#555";

// キャンバスを白色に塗る
ctx.fillStyle = 'rgb(255,255,255)';

// ストレージの初期化
var myStorage = localStorage;
window.onload = initLocalStorage();

// PC対応
canvas.addEventListener('mousedown', startPoint, false);
canvas.addEventListener('mousemove', movePoint, false);
canvas.addEventListener('mouseup', endPoint, false);
// スマホ対応
canvas.addEventListener('touchstart', startPoint, false);
canvas.addEventListener('touchmove', movePoint, false);
canvas.addEventListener('touchend', endPoint, false);

function startPoint(e){
  e.preventDefault();
  ctx.beginPath();
  
  // 矢印の先っぽから始まるように調整
  Xpoint = e.layerX;
  Ypoint = e.layerY;
  
  ctx.moveTo(Xpoint, Ypoint);
}

function movePoint(e){
  if(e.buttons === 1 || e.witch === 1 || e.type == 'touchmove')
  {
    Xpoint = e.layerX;
   Ypoint = e.layerY;
    moveflg = 1;
    
    ctx.lineTo(Xpoint, Ypoint);
    ctx.lineCap = "round";
    ctx.lineWidth = defSize * 2;
    ctx.strokeStyle = defColor;
    ctx.stroke();
    
  }
}

function endPoint(e)
{
    if(moveflg === 0)
    {
       ctx.lineTo(Xpoint-1, Ypoint-1);
       ctx.lineCap = "round";
       ctx.lineWidth = defSize * 2;
       ctx.strokeStyle = defColor;
       ctx.stroke();
       
    }
    moveflg = 0;
    setLocalStoreage();
}

function clearCanvas(){
    if(confirm('Canvasを初期化しますか?'))
    {
        initLocalStorage();
        temp = [];
        resetCanvas();
    }
}

function resetCanvas() {
    ctx.clearRect(0, 0, ctx.canvas.clientWidth, ctx.canvas.clientHeight);
    ctx.fillStyle = 'rgb(255,255,255)';
}

function chgImg()
{
  var png = canvas.toDataURL();

  document.getElementById("newImg").src = png;
}

function initLocalStorage(){
    myStorage.setItem("__log", JSON.stringify([]));
}
function setLocalStoreage(){
    var png = canvas.toDataURL();
    var logs = JSON.parse(myStorage.getItem("__log"));

    setTimeout(function(){
        logs.unshift({png:png});

        myStorage.setItem("__log", JSON.stringify(logs));

        temp = [];
    }, 0);
}

function prevCanvas(){
    var logs = JSON.parse(myStorage.getItem("__log"));

    if(logs.length > 0)
    {
        temp.unshift(logs.shift());

        setTimeout(function(){
            myStorage.setItem("__log", JSON.stringify(logs));
            resetCanvas();

            draw(logs[0]['png']);

        }, 0);
    }
}

function nextCanvas(){
    var logs = JSON.parse(myStorage.getItem("__log"));

    if(temp.length > 0)
    {
        logs.unshift(temp.shift());

        setTimeout(function(){
            myStorage.setItem("__log", JSON.stringify(logs));
            resetCanvas();

            draw(logs[0]['png']);

        }, 0);
    }
}

function draw(src) {
    var img = new Image();
    img.src = src;

    img.onload = function() {
        ctx.drawImage(img, 0, 0);
    }
}


              
            
!
999px

Console