Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: a3a5929e36
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 249 lines (234 sloc) 10.068 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
<DOCTYPE! html>
<html>
  <head>
    <title>try canvas</title>
    <style>
      body, textarea { font-family:monaco,monospace; }
      textarea {
        width:95%;
        border-radius:4px 4px;
        border:1px solid #eee;
        margin:1em 0 1em 2em;
        font-size:16px;
        min-height:100px;
      }
      #container {
        width:600px;
        margin:1em auto;
      }
      .code-canvas {
        margin:1em 0;
      }
      canvas {
        display:block;
        width:100%;
        height:300px;
        border:1px dashed #eee;
      }
      a:link, a:visited {
        color:#FC62CC;
        text-decoration:none;
      }
      a:hover {
        text-decoration:underline;
      }
      strong {
        color:#63E2FC;
        text-decoration:none;
      }
      .comment {
        color:#eee;
      }
      .indent {
        margin-left:1em;
      }
      #render-code {
        display:none;
      }
      #render-name, #draw-name {
        cursor:hand;
      }
      .drawfn .fill {
        display:none;
      }
    </style>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
  </head>
  <body>
    <div id="container">
      <h1>try canvas 「(゚ペ)&#9997;</h1>
      try <select id="examples"></select>
      <canvas width="600" height="300" id="canvas"></canvas>
      <strong>C-d</strong> to draw, <strong>C-c</strong> to clear, <strong>C-x</strong> for blank slate
      <div class="code-canvas drawfn">
         <div class="comment">/** draw on an open canvas</div>
          <div class="comment indent">* @param c the canvas's 2d context */</div>
          <div class="comment indent">* @param args js object for extra render data */</div>
        var <span id="draw-name">draw</span> = function(c, args) {
          <span class="fill">&#8230;</span>
          <textarea id="draw-code">//smile
c.beginPath();
c.arc(75,75,50,0,Math.PI*2,true);
c.moveTo(110,75);
c.arc(75,75,35,0,Math.PI,false);
c.moveTo(65,65);
c.arc(60,65,5,0,Math.PI*2,true);
c.moveTo(95,65);
c.arc(90,65,5,0,Math.PI*2,true);
c.stroke();</textarea>
        };
      </div>
      
      <div class="code-canvas renderfn">
         <div class="comment">/** render drawing, defaults to `draw(c, args);`</div>
          <div class="comment indent">* @param c the canvas's 2d context */</div>
                    <div class="comment indent">* @param args empty js object for extra render data */</div>
        var <span id="render-name">render</span> = function(c, args) {
          <span class="fill">&#8230;</span>
          <textarea id="render-code">draw(c, args);</textarea>
        };
      </div>
      <hr/>
      <foot><a href="http://twitter.com/softprops">@softprops</a> 2010 try <a href="http://diveintohtml5.org/canvas.html">canvas</a></foot>
    </div>
    
    <script type="text/javascript">
    
      (function(c){c.fn.growable=function(a){return this.each(function(){new jQuery.growable(this,a)})};c.growable=function(a,d){this.options=d||{min_height:2};this.textarea=jQuery(a);this.org_rows=a.rows;this.el=a;this.init()};c.growable.fn=jQuery.growable.prototype={};c.growable.fn.extend=jQuery.growable.extend=jQuery.extend;c.growable.fn.extend({init:function(){var a=this;c(this.textarea).css({overflow:"hidden"});this.textarea.keyup(function(){a.grow(this)});this.textarea.bind("paste",function(){setTimeout(a.grow(this),
      250)});a.grow(a.el)},grow:function(a){var d=a.value.split("\n"),b=d.length;d[b-1].length>0&&b++;if(b>a.rows)a.rows=this.options.max_height!="undefined"&&b>this.options.max_height?this.options.max_height:b;if(b<a.rows)a.rows=Math.max(Math.max(this.org_rows,1),b)}})})(jQuery);
 
      ;(function(d){var k=d.scrollTo=function(a,i,e){d(window).scrollTo(a,i,e)};k.defaults={axis:'xy',duration:parseFloat(d.fn.jquery)>=1.3?0:1};k.window=function(a){return d(window)._scrollable()};d.fn._scrollable=function(){return this.map(function(){var a=this,i=!a.nodeName||d.inArray(a.nodeName.toLowerCase(),['iframe','#document','html','body'])!=-1;if(!i)return a;var e=(a.contentWindow||a).document||a.ownerDocument||a;return d.browser.safari||e.compatMode=='BackCompat'?e.body:e.documentElement})};d.fn.scrollTo=function(n,j,b){if(typeof j=='object'){b=j;j=0}if(typeof b=='function')b={onAfter:b};if(n=='max')n=9e9;b=d.extend({},k.defaults,b);j=j||b.speed||b.duration;b.queue=b.queue&&b.axis.length>1;if(b.queue)j/=2;b.offset=p(b.offset);b.over=p(b.over);return this._scrollable().each(function(){var q=this,r=d(q),f=n,s,g={},u=r.is('html,body');switch(typeof f){case'number':case'string':if(/^([+-]=)?\d+(\.\d+)?(px|%)?$/.test(f)){f=p(f);break}f=d(f,this);case'object':if(f.is||f.style)s=(f=d(f)).offset()}d.each(b.axis.split(''),function(a,i){var e=i=='x'?'Left':'Top',h=e.toLowerCase(),c='scroll'+e,l=q[c],m=k.max(q,i);if(s){g[c]=s[h]+(u?0:l-r.offset()[h]);if(b.margin){g[c]-=parseInt(f.css('margin'+e))||0;g[c]-=parseInt(f.css('border'+e+'Width'))||0}g[c]+=b.offset[h]||0;if(b.over[h])g[c]+=f[i=='x'?'width':'height']()*b.over[h]}else{var o=f[h];g[c]=o.slice&&o.slice(-1)=='%'?parseFloat(o)/100*m:o}if(/^\d+$/.test(g[c]))g[c]=g[c]<=0?0:Math.min(g[c],m);if(!a&&b.queue){if(l!=g[c])t(b.onAfterFirst);delete g[c]}});t(b.onAfter);function t(a){r.animate(g,j,b.easing,a&&function(){a.call(this,n,b)})}}).end()};k.max=function(a,i){var e=i=='x'?'Width':'Height',h='scroll'+e;if(!d(a).is('html,body'))return a[h]-d(a)[e.toLowerCase()]();var c='client'+e,l=a.ownerDocument.documentElement,m=a.ownerDocument.body;return Math.max(l[h],m[h])-Math.min(l[c],m[c])};function p(a){return typeof a=='object'?a:{top:a,left:a}}})(jQuery);
      
      (function($) {
        var view = function(selector) { $.scrollTo(selector, 250); };
        
        var examples = [{
          name: "smiley",
          draw: '/* smile */ \n\
c.beginPath();\n\
c.arc(75,75,50,0,Math.PI*2,true);\n\
c.moveTo(110,75);\n\
c.arc(75,75,35,0,Math.PI,false);\n\
c.moveTo(65,65);\n\
c.arc(60,65,5,0,Math.PI*2,true);\n\
c.moveTo(95,65);\n\
c.arc(90,65,5,0,Math.PI*2,true);\n\
c.stroke();'
        }, {
          name: "bubble",
          draw:'/*bubble*/ \n\
var grad = c.createLinearGradient(0, 0, 100, 0);\n\
grad.addColorStop(0,"black");\n\
grad.addColorStop(0,"white");\n\
c.fillStyle=grad;\n\
c.beginPath();\n\
c.moveTo(75,25);\n\
c.quadraticCurveTo(25,25,25,62.5); \n\
c.quadraticCurveTo(25,100,50,100); \n\
c.quadraticCurveTo(50,120,30,125); \n\
c.quadraticCurveTo(60,120,65,100); \n\
c.quadraticCurveTo(125,100,125,62.5); \n\
c.quadraticCurveTo(125,25,75,25); \n\
c.fill();\n\
c.stroke();\n\
c.font = "bold 12px sans-serif";\n\
c.fillStyle = "#333";\n\
c.fillText("hi", 65, 70);'
        }, {
          name: 'octocat',
          draw:'/*octocat */ \n\
var oc = new Image(); \n\
oc.src = "http://support.github.com/discussions/site/2060/assets/1797555535fc6a50869a61fad614199c6b3320d1/octocat_happy_thumb.gif"; \n\
oc.onload = function() { \n\
for(var i = 0;i< 400;i++) {\n\
c.drawImage(oc, Math.floor(Math.random()*600+1), Math.floor(Math.random()*300+1)); \n\
}\n\
};'}, {
    name:'circles',
    draw:'/*circles*/ \n\
c.beginPath(); \n\
c.fillStyle = args.fill; \n\
c.arc(args.x,args.y,args.i,0,Math.PI*2,true);\n\
c.fill();',
  render:'var x = 600/2, \n\
y = 300/2, \n\
radius = 100, \n\
angle = 0, \n\
radians = function(deg) { return deg * Math.PI/180; }, \n\
color = function() { \n\
var col = (\n\
0xffffff * Math.random()\n\
).toString(16).\n\
replace(new RegExp("[.]"),""); \n\
return col.slice(0,6); \n\
}; \n\
\n\
for(var i=0; i < 30; i++) { \n\
args.x = x + Math.cos(radians(angle)) * radius; \n\
args.y = y + Math.sin(radians(angle)) * radius; \n\
args.fill = color(); \n\
args.i = i; \n\
draw(c, args); \n\
angle += 15; \n\
}'
      }];
        
        $("textarea").growable();
        
        var c = document.getElementById("canvas"), ctx = c.getContext("2d");
        
        $("#draw-name").click(function(e){
          e.preventDefault();
          $(".drawfn .fill").toggle();
          $("#draw-code").toggle();
          //view("#draw-name");
          return false;
        });
        
        $("#render-name").click(function(e){
          e.preventDefault();
          $(".renderfn .fill").toggle();
          $("#render-code").toggle();
          //view("#render-name");
          return false;
        });
       
        var onClear = function() {
          c.width = c.width;
        }, onDraw = function() {
          onClear();
          var drawCode = $("#draw-code").val(),
            renderCode = $("#render-code").val() || "draw(c, args);";
          $("canvas").css({"height":"300px"});
          try {
            eval("var draw = function(c, args) { " + drawCode + " };");
            eval("var render = function(c, args) { " + renderCode + " };");
            render(ctx, {});
            //view("#canvas");
          } catch (err) {
            alert(err);
          }
        }
        , onBlankSlate = function() {
          $("#draw-code, #render-code").val("");
        }, onInit = function(drawingCode, renderingCode) {
          $("#draw-code").val(drawingCode);
          $("#render-code").val(renderingCode || "draw(c, args);");
          onDraw();
        };
        
        document.onkeypress = function(e) {
          if(e.ctrlKey) {
            if(e.charCode === 4) { /* d */
              onDraw();
            } else if(e.charCode === 3) { /* c */
              onClear();
            } else if(e.charCode === 24) { /* x */
              onBlankSlate();
            }
          }
        };
        
        var select = $("#examples");
        select.change(function(e) {
          var name = select.val(),
            ex = examples.filter(function(e) {
              return e.name === name;
            })[0];
          onInit(ex.draw, ex.render);
        });
        
        for(var e in examples) {
          var ex = examples[e];
          $(select).append('<option name="'+ex.name+'">'+ex.name+'</option>');
        }

      })(jQuery);
    </script>
  </body>
</html>
Something went wrong with that request. Please try again.