HTML 5 canvas music symbols examples


Symbol and code
Original 1/128 note stem up image

Original 1/128 note stem up
Canvas rendered 1/128 note stem up



      
Download file


Line 1         :  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
Line 2         :      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Line 3         :  <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
Line 4         :    <head> 
Line 5         :      <title>Mobilefish.com - 1/128 note stem up HTML 5 canvas example</title>
Line 6         :  
Line 7         :      <!--
Line 8         :      Music symbol 1/128 note stem up v. 1.0
Line 9         :      Author: Robert Lie
Line 10       :      www.mobilefish.com
Line 11       :      
Line 12       :      This file is public domain. You can use it for any purpose without restriction.
Line 13       :      I do not guarantee that it is correct, so use it at your own risk. 
Line 14       :      If you use it for something interesting, I'd appreciate hearing about it. 
Line 15       :      If you find any bugs or make any improvements, I'd appreciate hearing about those too.
Line 16       :      It would also be nice if my name and URL were left in the comments. But none of that is required.
Line 17       :  
Line 18       :      Requires: File excanvas.compiled.js 
Line 19       :                HTML5 Canvas for Internet Explorer
Line 20       :                See: http://code.google.com/p/explorercanvas/
Line 21       :      -->
Line 22       :  
Line 23       :      <!--[if IE]>
Line 24       :      <script type="text/javascript" src="scripts/excanvas.compiled.js"></script>
Line 25       :      <![endif]-->
Line 26       :      <script type="text/javascript">
Line 27       :  
Line 28       :      function drawShape(){
Line 29       :          var canvas = document.getElementById('cssxCoordinates');
Line 30       :          if (canvas.getContext){
Line 31       :              var ctx = canvas.getContext('2d');
Line 32       :              
Line 33       :              var x = 11; 
Line 34       :              var y = 363;
Line 35       :        
Line 36       :              drawNote128(ctx,x,y);
Line 37       :                            
Line 38       :          } else {
Line 39       :              alert('You need Firefox 1.5+, Google Chrome 8.0+, Internet Explorer 8.0+ or Safari 4.0+ to see the example.');
Line 40       :          }
Line 41       :      }
Line 42       :      
Line 43       :      function drawNote128(ctx,x,y){       
Line 44       :        drawNoteHeadClosed(ctx,x,y);
Line 45       :        drawNoteStemUp(ctx,x,y);
Line 46       :        drawNoteFlagDown(ctx,x,y);
Line 47       :      }
Line 48       :      
Line 49       :      function drawNoteHeadClosed(ctx,x,y){
Line 50       :      
Line 51       :        var x_offset_head = 12; 
Line 52       :        var y_center_head = 74; 
Line 53       :        
Line 54       :        var x_corner = x - x_offset_head;
Line 55       :        var y_corner = y - y_center_head;
Line 56       :        
Line 57       :        ctx.save();
Line 58       :        
Line 59       :        ctx.translate(x_corner,y_corner);
Line 60       :        ctx.fillStyle = "black";
Line 61       :        
Line 62       :        ctx.beginPath();
Line 63       :        ctx.moveTo(13,86);
Line 64       :   
Line 65       :        ctx.quadraticCurveTo(13,118,42,126);
Line 66       :        ctx.quadraticCurveTo(67,129,101,108);
Line 67       :        ctx.quadraticCurveTo(136,85,142,57);
Line 68       :        ctx.quadraticCurveTo(142,28,112,19);
Line 69       :        ctx.quadraticCurveTo(88,16,54,36);
Line 70       :        ctx.quadraticCurveTo(19,59,13,86);
Line 71       :        ctx.fill();
Line 72       :    
Line 73       :        ctx.restore();    
Line 74       :      }
Line 75       :      
Line 76       :      function drawNoteStemUp(ctx,x,y){
Line 77       :      
Line 78       :        var x_delta_stem = 133; 
Line 79       :        var y_delta_stem = 36;  
Line 80       :        
Line 81       :        var x_offset_stem = 138;
Line 82       :        var y_offset_stem = 327;
Line 83       :            
Line 84       :        var x_corner = x + x_delta_stem - x_offset_stem; 
Line 85       :        var y_corner = y - y_delta_stem - y_offset_stem;
Line 86       :              
Line 87       :        ctx.save();
Line 88       :        
Line 89       :        ctx.translate(x_corner,y_corner);
Line 90       :            
Line 91       :        ctx.fillStyle = "black";
Line 92       :        ctx.beginPath();
Line 93       :        ctx.moveTo(127,4);
Line 94       :        ctx.lineTo(135,4);
Line 95       :        ctx.lineTo(135,342);
Line 96       :        ctx.lineTo(127,342);
Line 97       :        ctx.fill();   
Line 98       :        
Line 99       :        ctx.restore();  
Line 100     :      }   
Line 101     :  
Line 102     :      function drawNoteFlagDown(ctx,x,y){
Line 103     :  
Line 104     :        var number_of_flags = 5;
Line 105     :     
Line 106     :        var x_delta_stem = 133; 
Line 107     :        var y_delta_stem = 36;  
Line 108     :        
Line 109     :        var stem_height = 338;
Line 110     :     
Line 111     :        var x_offset_flag = 17;
Line 112     :        var y_offset_flag = 12;
Line 113     :            
Line 114     :        var x_corner = x + x_delta_stem - x_offset_flag; 
Line 115     :        var y_corner = y - y_delta_stem - stem_height + y_offset_flag;
Line 116     :        
Line 117     :        var flag_distance = 40;
Line 118     :     
Line 119     :        for(var i=1; i<=number_of_flags; i++) {
Line 120     :          ctx.save();
Line 121     :          if(i==1) {
Line 122     :            ctx.translate(x_corner,y_corner);
Line 123     :          } else {
Line 124     :            ctx.translate(0,flag_distance);
Line 125     :          }
Line 126     :          
Line 127     :          ctx.fillStyle = "black";
Line 128     :          
Line 129     :          ctx.beginPath();
Line 130     :          ctx.moveTo(14,54);    
Line 131     :          ctx.quadraticCurveTo(14,32,14,9);
Line 132     :          ctx.quadraticCurveTo(24,27,40,38);
Line 133     :          ctx.quadraticCurveTo(56,50,85,73);
Line 134     :          ctx.quadraticCurveTo(101,85,110,102);
Line 135     :          ctx.quadraticCurveTo(115,116,113,131);
Line 136     :          ctx.quadraticCurveTo(112,131,111,131);
Line 137     :          ctx.quadraticCurveTo(111,116,102,104);
Line 138     :          ctx.quadraticCurveTo(76,74,39,61);
Line 139     :          ctx.quadraticCurveTo(27,57,14,54);
Line 140     :          ctx.fill();     
Line 141     :        }
Line 142     :              
Line 143     :        ctx.restore();        
Line 144     :      }
Line 145     :      
Line 146     :      </script>
Line 147     :    </head>
Line 148     :    <body onload="drawShape();">
Line 149     :      <h1>1/128 note stem up</h1>
Line 150     :      <canvas id="cssxCoordinates" width="300" height="435"></canvas> 
Line 151     :    </body>
Line 152     :  </html>