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

Quick-add: + add another resource

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.

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

Console