Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs 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 its URL and the proper URL extension.

+ 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

Auto Save

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="canvas" width="500" height="3500"></canvas>
              
            
!

CSS

              
                body {
  text-align: center;
}
* {
  margin: 0;
  padding: 0;
}

              
            
!

JS

              
                window.onload = () => {
  // キャンパスの要素を取得
  const canvas = document.getElementById("canvas");
  // 2次元の描画を行うメソッド
  const ctx = canvas.getContext("2d");
  // 座標とcanvasサイズを入れる用の変数
  let x = 0;
  let y = 0;
  let w = canvas.width;
  let h = canvas.height;
  // 背景塗る
  ctx.fillStyle = "#ccc";
  // fillRect(x座標, y座標, 幅, 高さ)
  ctx.fillRect(0, 0, w, h);

  // es6以前の書き方:function onClick (e) {…}
  let onClick = (e) => {
    let rect = e.target.getBoundingClientRect();
    // e.clientX(Y)がwindow左上からのクリックされた座標
    // rect.left(top)がwindow左上からのcanvasの座標
    x = e.clientX - rect.left;
    y = e.clientY - rect.top;
    console.log("e.clientY " + e.clientY);
    console.log("rect.top " + rect.top);
    console.log("y " + y);
    draw();
  };

  // function draw() {…}
  const draw = () => {
    // canvasで背景色つけてるとclearRectで初期化すると真っ白になっちゃうのでfillRectで塗りつぶし
    ctx.fillStyle = "#ccc";
    ctx.fillRect(0, 0, w, h);
    // 背景色が真っ白ならclearRectでもいいと思う
    // ctx.clearRect(0, 0, w, h);

    // クリックされた位置に描画処理
    ctx.fillStyle = "#000";
    // fillRect(x座標, y座標, 幅, 高さ)
    ctx.fillRect(x, y, 10, 10);
  };
  canvas.addEventListener("click", onClick, false);
};

              
            
!
999px

Console