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="ctx" width="400" height="400"></canvas>
              
            
!

CSS

              
                
              
            
!

JS

              
                ;(()=>{
	"use strict";
	/**
	 * キャンバスオブジェクト
	 * @param {string} canvasId canvasのid属性
	 */
	class Canvas{
		constructor(canvasId) {
			var canvas  = document.getElementById(canvasId);
			this.ctx    = canvas.getContext('2d');
			this.canvasW = canvas.width;
			this.canvasH = canvas.height;
		}
		clear(){
			this.ctx.clearRect(0, 0, this.canvasW , this.canvasH );
		}
	}
	/**
	 * 座標オブジェクト
	 * @param {number} x X座標
	 * @param {number} y Y座標
	 */
	class Point{
		constructor(x,y) {
			this.x = x;
			this.y = y;
		}
	}
	/**
	 * 多角形オブジェクト
	 * @param {Array} points 座標情報
	 */
	class Polygon{
		constructor(points) {
			this.points = points;
		}
		draw(ctx,strokeStyle,fillStyle){
			ctx.beginPath();
			ctx.moveTo(this.points[0].x, this.points[0].y);
			for(var i=1; i<this.points.length ; i++){
				ctx.lineTo(this.points[i].x, this.points[i].y);
			}
			ctx.closePath();
			if(strokeStyle){
				ctx.strokeStyle = strokeStyle;
				ctx.stroke();
			}
			if(fillStyle){
				ctx.fillStyle   = fillStyle;
				ctx.fill();
			}
		}
	}
	/**
	 * 三角形オブジェクト
	 * @param {Array} points 座標情報
	 */
	class Triangle extends Polygon{
		/**
		* 外接円の取得
		*/
		getCircumscribedCircle() {
			//未定義の場合は定義
			if(!this._circumscribedCircle){
				var x1 = this.points[0].x;
				var y1 = this.points[0].y;
				var x2 = this.points[1].x;
				var y2 = this.points[1].y;
				var x3 = this.points[2].x;
				var y3 = this.points[2].y;
				
				//各座標の2乗を取得
				var x1pow2 = Math.pow(x1,2);
				var x2pow2 = Math.pow(x2,2);
				var x3pow2 = Math.pow(x3,2);
				var y1pow2 = Math.pow(y1,2);
				var y2pow2 = Math.pow(y2,2);
				var y3pow2 = Math.pow(y3,2);
				
				//公式より中心座標を取得
				var c = 2 * ((x2 - x1) * (y3 - y1) - (y2 - y1) * (x3 - x1));
				var x = ((y3 - y1) * (x2pow2 - x1pow2 + y2pow2 - y1pow2) + (y1 - y2) * (x3pow2 - x1pow2 + y3pow2 - y1pow2)) / c;
				var y = ((x1 - x3) * (x2pow2 - x1pow2 + y2pow2 - y1pow2) + (x2 - x1) * (x3pow2 - x1pow2 + y3pow2 - y1pow2)) / c;
				
				//外接円の半径rは、中心から三角形の任意の頂点(x1,y1)までの距離
				var r = Math.sqrt(Math.pow(x1 - x,2) + Math.pow(y1 - y,2));
				this._circumscribedCircle =  new Circle(new Point(x,y),r);
			}
			return this._circumscribedCircle;
		}
	}
	/**
	 * 円オブジェクト
	 * @param {Point} center 中心座標情報
	 * @param {number} radius 半径
	 */
	class Circle{
		constructor(center,radius){
			this.center = center;
			this.radius = radius;
		}
		/**
		* 描画
		*/
		draw(ctx,strokeStyle,fillStyle){
			ctx.beginPath();
			ctx.arc(this.center.x,this.center.y,this.radius,0,Math.PI*2,true);
			if(strokeStyle){
				ctx.strokeStyle = strokeStyle;
				ctx.stroke();
			}
			if(fillStyle){
				ctx.fillStyle   = fillStyle;
				ctx.fill();
			}
		}
	};
	//表示処理
	var canvas = new Canvas("ctx");
	canvas.clear();

	//ステージ情報
	var stage = {
		x : 50,
		y : 50,
		w : 100,
		h : 100
	}

	//ステージを描画
	stage.obj = new Polygon([
		new Point(stage.x         , stage.y        ) , 
		new Point(stage.x+stage.w , stage.y        ) , 
		new Point(stage.x+stage.w , stage.y+stage.h) , 
		new Point(stage.x         , stage.y+stage.h) 
	]);
	stage.obj.draw(canvas.ctx,"blue");

	//ステージ内にランダムな3ポイントを取得
	var trianglePoints = [];
	for (var i = 0; i < 3; i++) {
		trianglePoints.push(new Point(
			stage.x+Math.random() * stage.w , 
			stage.y+Math.random() * stage.h
		));
	}

	//三角形を描画
	var triangle = new Triangle(trianglePoints);
	triangle.draw(canvas.ctx,"green");
	
	//三角形の外接円を描画
	triangle.getCircumscribedCircle().draw(canvas.ctx,"red");

})();
              
            
!
999px

Console