cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

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.

            
              let elem = document.getElementById('canvas');
elem.width = 300;
elem.height = 300;
let ctx = elem.getContext('2d');

// 原点oをキャンバスの中心点に置く
let oX = elem.clientWidth/2;
let oY = elem.clientHeight/2;

// 原点から計算した点を返す
const calcPosX = function(x) {
  return oX + x;
};
const calcPosY = function(y) {
  return oY - y;
}

ctx.beginPath();
// 原点へ移動する
ctx.moveTo(oX, oY);
// 原点から(40, 60)の位置までのベクトルを描く
ctx.lineTo(calcPosX(40), calcPosY(60));
ctx.closePath();
ctx.stroke();

// (40, 60)のベクトルに直行する線を表現すると
// (-60, 40)のベクトルになるが、矢印にするには長いので短くしておく
// ((-60 / 4), (40 / 4))
let arrowX = -15;
let arrowY = 10;

// 左側の三角形を作る
ctx.beginPath();
// (40, 60)の点まで移動
ctx.moveTo(oX + 40, oY - 60);
// (40, 60)を原点としたベクトルを描く
// FIXME 仕様を正しく反映してないのを直す
ctx.lineTo(calcPosX(arrowX) + 40, calcPosY(arrowY) - 60);

// (40, 60)から少しだけ長くしたとこまでのベクトルを描く
ctx.lineTo(oX + 40 + 10, oY - 60 - 15);
ctx.closePath();
ctx.stroke();
ctx.fill();

// 右側の三角形を作る
// 左側の三角形と同じことを、ベクトルを反転させて行う
ctx.beginPath();
ctx.moveTo(oX + 40, oY - 60);
ctx.lineTo(calcPosX(-arrowX) + 40, calcPosY(-arrowY) - 60);
ctx.lineTo(oX + 40 + 10, oY - 60 - 15);
ctx.closePath();
ctx.stroke();
ctx.fill();

            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console