ラベル javascript の投稿を表示しています。 すべての投稿を表示
ラベル javascript の投稿を表示しています。 すべての投稿を表示

2013年4月28日日曜日

Strokesというストロークを再生出来るお絵描きWebアプリ的なのを作りました

このエントリーをブックマークに追加 このエントリーを含むはてなブックマーク

はじめに


 2日ほど前の夜中に@kojira@hamatzらが

といった感じで語らっていたので、

 という事で作りました。ものはこちらです。HTML5的なWebアプリです。

Strokes


できる事


 大体以下の事が出来ます。メインはトレースモードでしょうか。書いた絵を1ストロークずつ再生し、それをなぞる感じです。 
  • 線画を書く
  • アンドゥ・リドゥ
  • 絵を保存する(localStorage)
  • 保存した絵の再編集
  • 保存した絵の削除
  • 書いた絵のストロークを再生
  • 絵のトレースモード


  • デモ


    詳しくは以下の動画を。絵描きさんの絵をトレースモードでなぞって練習とか色々捗るんじゃないかなと。



    おわりに


     まだ排他処理やってなかったりだとか、サーバへのデータ保存出来なかったりだとか、ペイントツール部分もしょぼかったりします。なんか要望とかアイデアあればStrokes Issuesに書いて下さい。あわよくばpull request下さい。あとUIデザイン周りと、コンテンツとして良さげな線画書いてくれる方募集。

    2012年1月6日金曜日

    Javascriptによるドラゴン曲線。辰年だし。

    このエントリーをブックマークに追加 このエントリーを含むはてなブックマーク

    辰年だしドラゴン曲線だよね。という事でjavascriptで。Canvasで。

    新年あけましてドラゴン曲線 - jsdo.it - share JavaScript, HTML5 and CSS


    ソース

    <html>
    <script>
    window.onload =function(){
     var $ = function(id){ return document.getElementById(id); }
     var canvas = $("canvas");
     var ctx = canvas.getContext('2d');
     var clear =function(){
      ctx.fillStyle = 'rgb(200, 200, 200)';
      ctx.fillRect(0, 0, canvas.width, canvas.height);
     }
     var line = function(ctx, sx, sy, ex, ey){
      ctx.beginPath();
      ctx.moveTo(sx, sy);
      ctx.lineTo(ex, ey);
      ctx.closePath();
      ctx.stroke();
     }
     var dline = function(ctx, sx, sy, ex, ey, cx, cy){
      ctx.beginPath();
      ctx.moveTo(sx, sy);
      ctx.lineTo(cx, cy);
      ctx.moveTo(ex, ey);
      ctx.lineTo(cx, cy);
      ctx.closePath();
      ctx.stroke();
     };
     var dragon = function(ctx, depth, sx, sy, ex, ey, d){
      if(depth ==0){
       line(ctx,sx,sy,ex,ey);
       return;
      }
      var dx = (ex-sx)/2;
      var dy = (ey-sy)/2;
      var centerX = ex-dx;
      var centerY = ey-dy;
      var cx = centerX + (d?dy:-dy);
      var cy = centerY + (d?-dx:dx);
      if(depth == 1){
       dline(ctx,sx,sy,ex,ey,cx,cy);
       return;
      }
      else{
       dragon(ctx, depth-1, sx, sy, cx, cy, true);
       dragon(ctx, depth-1, cx, cy, ex, ey, false);
      }
     }
     var dragon_call = function(depth){
      return function(){
       clear();
       dragon(ctx, depth, 50, canvas.height/3, canvas.width-100, canvas.height/3,false);
       if(depth < 15){
        setTimeout(dragon_call(depth+1), 1000);
       }
      }
     };
     $("start").onclick = function(){
      dragon_call(0)();
     }
     dragon_call(15)();
    }
    </script>
    <body>
    
    <canvas id="canvas" width=400 height=400 ></canvas>
    <br/>
    <input type="button" id="start" value="start" ></input>
    </body>
    </html>
    

    まとめ


    今年も頑張る。

    2011年11月7日月曜日

    CoffeeScriptでライフゲーム作った

    このエントリーをブックマークに追加 このエントリーを含むはてなブックマーク
    CoffeeScriptおもしれー
    という事でライフゲームを作成しました。
    HTML側はもう書かずエレメントの生成も全部CoffeeScriptでやってみました。

    ライフゲーム


    lifegame - jsdo.it - share JavaScript, HTML5 and CSS


    コード


    ライフゲームのルールはWikipediaで→ライフゲーム - Wikipedia

    CoffeeScriptの実装はこの辺を適当に参照しつつ。

    #util methods
    create = (type) -> document.createElement(type)
    append = (child) -> document.body.appendChild(child)
    br = -> append(create("br"))
    text= (type, text) ->
     elm = create(type)
     elm.innerHTML = text
     append(elm)
    input = (type, value) ->
     elm = create("input")
     elm.type=type
     elm.value = value
     elm
    button= (value, fn) ->
     elm = input("button", value)
     elm.onclick = fn
     append(elm)
     
    #consts
    CELL_SIZE = 5
    BOARD_SIZE = 350
    LENGTH = BOARD_SIZE/CELL_SIZE
    
    isStart = false
    
    cells = new Array(Math.pow(LENGTH, 2))
    for i in [0.. cells.length-1]
     cells[i] = {"now":0, "next":0}
    
    #methods
    draw = (canvas, context) ->
     context.fillStyle="#eeeeee"
     context.fillRect(0,0, canvas.width, canvas.height)
     context.fill()
     
     context.fillStyle="#000000"
     context.strokeStyle="#000000"
     context.lineWidth = 1
     x = canvas.width/CELL_SIZE
     y = canvas.height/CELL_SIZE
     for i in [0.. x]
      context.beginPath()
      context.moveTo(i*CELL_SIZE , 0)
      context.lineTo(i*CELL_SIZE , canvas.height)
      context.stroke()
     for j in [0.. y]
      context.beginPath()
      context.moveTo(0, j*CELL_SIZE)
      context.lineTo(canvas.width, j*CELL_SIZE)
      context.stroke()   
     for c,i in cells
      if c.now == 1
       x = Math.floor(i % LENGTH)
       y = Math.floor(i / LENGTH)
       context.fillRect(x*CELL_SIZE, y*CELL_SIZE, CELL_SIZE, CELL_SIZE)
    
    initialize = ->
     canvas = document.createElement("canvas")
     canvas.width = BOARD_SIZE
     canvas.height = BOARD_SIZE
     context = canvas.getContext("2d")
     canvas.onmousedown = (e) -> 
      x = Math.floor(e.offsetX / CELL_SIZE)
      y = Math.floor(e.offsetY / CELL_SIZE)
      if cells[x+(y*LENGTH)].now == 0
       cells[x+(y*LENGTH)].now = 1
      else
       cells[x+(y*LENGTH)].now = 0
      draw(canvas, context)
     append(canvas)
     draw(canvas, context)
     br()
     text("span","speed")
     speed = input("text", "10")
     speed.size = 10
     append(speed)
     br()
     button("start", ->
      if !isStart 
       isStart = true
       start(speed.value)
     )
     button("stop", ->
      isStart = false  
     )
     button("one step", ->
      if !isStart
       start(speed.value)
     )
     button("reset", ->
      for i in [0.. cells.length-1]
       cells[i] = {"now":0, "next":0}
      draw(canvas, context)
     )
     button("random", ->
      for i in [0.. cells.length-1]
       if Math.floor(Math.random() * 10) + 1 > 3
        cells[i].now = 0
       else
        cells[i].now = 1
      draw(canvas, context)
     )
     start = (speed) ->
      calc()
      draw(canvas, context)
      if isStart
       setTimeout(-> 
        start(speed)
       , 1000/speed)
     calc = ->
      for c, i in cells
       count = add(i-1)
       count+=add(i+1)
       count+=add(i-1-LENGTH)
       count+=add(i+1-LENGTH)
       count+=add(i-LENGTH)
       count+=add(i-1+LENGTH)
       count+=add(i+1+LENGTH)
       count+=add(i+LENGTH)
       switch count
        when 0,1,4,5,6,7,8
         c.next = 0
        when 3
         c.next = 1
        when 2
         c.next = c.now
      for c in cells
       c.now = c.next
     add = (index)->
      if index >= 0 && index < cells.length
       cells[index].now
      else
       0
    window.onload= ->
     initialize()
    
    
    

    結論


    CoffeeScriptいい!