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.

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.

HTML

            
              <canvas id="cvs" width="450" height="440"></canvas>
            
          
!

CSS

            
              body { background-color: #FFFFFF; }
            
          
!

JS

            
              // forked from siouxcitizen's "動かない。。。 作業用に保存" http://jsdo.it/siouxcitizen/cioT
//前に自分が作成した「canvasで↑↓←→ボタンで組み込み画像の移動」
//http://jsdo.it/siouxcitizen/zUz9
//で画像が表示されなくなってしまったので、
//かわりにbase64画像(鯖いらず)によるものを作成してみました
//↑Firefox上の動作のみ確認してます
//
//以下コードを参考にさせて頂きました
//forked from: タイル。失敗。
//http://jsdo.it/paq/vfWd
//
//以下コードをbase64画像への変換に使用させていただきました
//2009-3-14 選んだファイルを Base64 エンコードする
//http://wonderfl.net/c/h1NU
var img = new Image();
var ctx;
var xPos = 0;
var yPos = 0;

var data = "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAQDAwQDAwQEAwQFBQQFBwwHBwYGBw4KCwgMEQ8SEhEPEBATFRsXExQaFBAQGCAYGhwdHh8eEhchJCEeJ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}

function draw() {
  var canvas = document.getElementById("cvs");
  if (!canvas) return false;
  canvas.width = 450;
  canvas.height = 440;
  ctx= canvas.getContext("2d");
  if(!ctx) return false;
  img = new Image();
  img.src = data;
  img.onload=function() {
    ctx.drawImage(img,0,0);
  }
  //キープレスイベントを処理
  document.addEventListener('keydown', handleKeyPress, false);
}

function handleKeyPress(event) {
  if (event.keyCode==38) {//↑ボタン押下
    if (yPos > 0) {yPos--;}
    ctx.clearRect(0,0,450,440); //一度描画部分をまっさらにしてから
    ctx.drawImage(img, 10*xPos, 10*yPos); //改めて画像を書き直す
  } else if (event.keyCode==40) {//↓ボタン押下
    if (yPos < 31) {yPos++;}
    ctx.clearRect(0,0,450,440);
    ctx.drawImage(img, 10*xPos, 10*yPos);
  } else if (event.keyCode==39) {//→ボタン押下
    if (xPos < 31) {xPos++;}
    ctx.clearRect(0,0,450,440);
    ctx.drawImage(img, 10*xPos, 10*yPos);
  } else if (event.keyCode==37) {//←ボタン押下
    if (xPos > 0) {xPos--;}
    ctx.clearRect(0,0,450,440);
    ctx.drawImage(img, 10*xPos, 10*yPos);
  }
}
            
          
!
999px

Console