適当に哲学書とやらを読んでみる

天の孔雀的、英語の本かなり意訳読み

全体表示

[ リスト ]

html5canvas要素で楕円を描くオブジェクトを作ってみました。
 
canvas要素で楕円を描きたくてうずうずしている人に朗報。
canvas要素で楕円を描くオブジェクトを作ってみました。
とりあえず、わからなければコピペすべしっ。
そして、下の書き方例にそってコーディングすべしっ。
もしバグがあったら教えてください。

Ellipse= {
  "init" : function(ctx, centerX,centerY, xSize, ySize){
    var elps = this.ellipse;
    elps.value = -360;
    elps.ctx = ctx;
    elps.cx = centerX;
    elps.cy = centerY;
    elps.x = xSize;
    elps.y = ySize;
    elps.drawn = false;
    return elps;
  },
  "ellipse" : {
    "value" : 360,
    "x" : 0,
    "y" : 0,
    "cx" : 0,
    "cy" : 0,
    "ctx" : null,
    "drawn" : false,
    "isWidthLarge" :function(){return ((this.x > this.y) ? true : false);},
    "baseRadius" : function(){
      if(this.isWidthLarge()){
        return this.y;
      }else{
        return this.x;
      }
    },
    "moveRadius" : function(){
      if(this.isWidthLarge()){
        return (this.x - this.y) * (this.x +this.y);
      }else{
        return (this.y - this.x) * (this.y +this.x);
      }
    },
    "moveNext" : function(){
      this.value += 1;
      return this.value < 360;
    },
    "getPosRadian" : function(){
      return (this.value * Math.PI) / 360;
    },
    "getPosRadius" : function(){
      returnMath.sqrt(Math.pow(this.baseRadius(), 2) + this.moveRadius() *Math.pow(this.movePosRadian(), 2));
    },
    "getPosX" : function(){
      return this.x *Math.cos(this.getPosRadian());
    },
    "getPosPhy" : function(){
      if(this.value >= 0){
        return Math.acos(this.getPosX() /this.getPosRadius());
      }else{
        return - Math.acos(this.getPosX() /this.getPosRadius());
      }
    },
    "movePosRadian" : function(){
      var rtn;
      if(this.isWidthLarge()){
        if(Math.cos(this.getPosRadian()) <0){
          rtn = -Math.cos(this.getPosRadian());
        }else{
          rtn = Math.cos(this.getPosRadian());
        }
      }else{
        if(Math.sin(this.getPosRadian()) <0){
          rtn = -Math.sin(this.getPosRadian());
        }else{
          rtn = Math.sin(this.getPosRadian());
        }
      }
      return rtn;
    },
    "getNextRadian" : function(){
      return ((this.value + 1) * Math.PI) /360;
    },
    "getNextRadius" : function(){
      returnMath.sqrt(Math.pow(this.baseRadius(), 2) + this.moveRadius() *Math.pow(this.moveNextRadian(), 2));
    },
    "getNextX" : function(){
      return this.x *Math.cos(this.getNextRadian());
    },
    "getNextPhy" : function(){
      if(this.value >= 0){
        return Math.acos(this.getNextX() /this.getNextRadius());
      }else{
        return - Math.acos(this.getNextX() /this.getNextRadius());
      }
    },
    "moveNextRadian" : function(){
      var rtn;
      if(this.isWidthLarge()){
        if(Math.cos(this.getNextRadian()) <0){
          rtn = -Math.cos(this.getNextRadian());
        }else{
          rtn = Math.cos(this.getNextRadian());
        }
      }else{
        if(Math.sin(this.getNextRadian()) <0){
          rtn = -Math.sin(this.getNextRadian());
        }else{
          rtn = Math.sin(this.getNextRadian());
        }
      }
      return rtn;
    },
    "draw" : function(){
      this.ctx.beginPath();
      while(this.moveNext())
        this.drawArc();
      this.drawn = true;
    },
    "drawArc" : function(ctx, ox, oy,r, from, to){
      var start = - this.getPosPhy();
      var end = - this.getNextPhy();
      if(this.ctx){
        this.ctx.arc(this.cx, this.cy,this.getPosRadius(), start, end, true);
        return true;
      }else{
        return false;
      }
    },
    "fill" : function(style){
      if(this.drawn){
        this.ctx.fillStyle = style;
        this.ctx.fill();
        return true;
      }else{
        return false;
      }
    },
    "stroke" : function(style){
      if(this.drwan){
        this.ctx.strokeStyle = style;
        this.ctx.stroke();
        return true;
      }else{
        return false;
      }
    }
  }
}
 
長いっ!
 
その分、使い方は簡単にしております。
そうでなければ、労は報われない…
 
【使い方-例】
Idcanvasという値を設定したcanvas要素を準備しておく。
Canvas要素のwidth属性とheight属性にはそれぞれ、300を指定しておく。
 
1
var cvs = $("canvas");
2
var ctx = cvs.getContext("2d");
3
var elps = Ellipse.init(ctx, 150, 150, 150, 100);
4
elps.draw();
5
elps.fill("rgba(204, 204, 255, 0.7)");
6
elps.stroke("rgba(0, 51, 153, 1)");
 
1と行2canvas要素を2Dグラフィックスとして描くための設定。
3は今回作成したオブジェクトの初期化処理。引数は以下の通り。
Ellipse.init( Canvas2DContext, centerX, centerY, radiusX, radiusY )
Canvas2DContext
Canvas2Dコンテキストを渡す。
centerX
楕円の中心位置のX座標
centerY
楕円の中心位置のY座標
radiusX
楕円の横方向の長さ
radiusY
楕円の縦方向の長さ
4は楕円を描く処理。引数なし。
5は円を塗りつぶす処理。
引数は色を表す文字列(例:”#ff9900”とか、”red”など)
6は円のボーダーラインを引く処理。
引数は色を表す文字列。
 
ちなみにこの処理の実行結果は次の通りです。
 

イメージ 1


 

よしもとブログランキング

もっと見る

プライバシー -  利用規約 -  メディアステートメント -  ガイドライン -  順守事項 -  ご意見・ご要望 -  ヘルプ・お問い合わせ

Copyright (C) 2019 Yahoo Japan Corporation. All Rights Reserved.

みんなの更新記事