夏の暑さに負けるのは困るけど、勝てる見込みもなく。
こんにちは、muchoです。
以前、若きホープのきょんたろ君がアップしたHtml5 Canvas Templete ver.1を
クラスっぽく作り直してみました。
ついでにcoffee scriptで書いてみました。
Processing.jsを試してみたんですが、IE8でエラーが出て動かなかったため、
簡易Processing風なテンプレートを自作することにした次第です。
そんな背景があるので、記述の仕方はProcessingになるべくあわせています。
基本となるCanvasクラスを拡張して書くことを前提にしています。
coffee scriptだとこんな感じ
01 | class Canvas1 extends Canvas |
04 | @fillColor = "#202020" |
05 | @strokeColor = 'rgba(0, 0, 0, 0.1)' |
06 | @backgroundColor = 'rgb(240, 240, 240)' |
16 | pointNum = ( @mouseX / @width * @pointMax + @pointMin ) |
17 | @Num + = ( pointNum - @Num ) * 0.1 |
28 | @line points [ i ] [ 0 ] , points [ i ] [ 1 ] , points [ j ] [ 0 ] , points [ j ] [ 1 ] |
吐き出されたJSはこんな感じ
03 | __hasProp = {}.hasOwnProperty, |
04 | __extends = function (child, parent) { for ( var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this .constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; |
06 | Canvas1 = ( function (_super) { |
08 | __extends(Canvas1, _super); |
11 | return Canvas1.__super__.constructor.apply( this , arguments); |
14 | Canvas1.prototype.setup = function () { |
15 | this .fillColor = "#202020" ; |
16 | this .strokeColor = 'rgba(0, 0, 0, 0.1)' ; |
17 | this .backgroundColor = 'rgb(240, 240, 240)' ; |
18 | this .pi = Math.PI * 2; |
25 | Canvas1.prototype.update = function () { |
27 | pointNum = this .mouseX / this .width * this .pointMax + this .pointMin; |
28 | return this .Num += (pointNum - this .Num) * 0.1; |
31 | Canvas1.prototype.draw = function () { |
32 | var i, j, l, points, _results; |
34 | points = this .getPoints(); |
41 | this .line(points[i][0], points[i][1], points[j][0], points[j][1]); |
48 | this .Canvas1 = Canvas1; |
ソースをGithubにアップしてみました。まだ慣れてないのでgitドキドキ
https://github.com/devjam/canvas.js