{"id":309,"date":"2011-05-24T07:58:52","date_gmt":"2011-05-24T07:58:52","guid":{"rendered":"http:\/\/www.osd.net\/blog\/?p=309"},"modified":"2011-05-24T11:57:57","modified_gmt":"2011-05-24T11:57:57","slug":"create-a-css-like-button-with-hmtl5-canvas-element-using-gradient","status":"publish","type":"post","link":"https:\/\/www.osd.net\/blog\/web-development\/css\/create-a-css-like-button-with-hmtl5-canvas-element-using-gradient\/","title":{"rendered":"Create a CSS-like button with HMTL5 canvas element using gradient"},"content":{"rendered":"<p><strong>Canvas<\/strong> is maybe one of the most interesting <strong>HTML5<\/strong> feature. It has all you need to create rich web applications or games.<\/p>\n<p>We talked about how to <a href=\"https:\/\/www.osd.net\/blog\/web-development\/php\/generate-gradient-image-with-php\">Generate gradient image with PHP<\/a> some time ago.<br \/>\nNow we&#8217;re gonna use HMTL5 canvas element to draw a simple button with a gradient effect, using it&#8217;s 2D context API.<\/p>\n<p>First step is to create a simple html file with a canvas element(we set the width to 100 pixels and height to 50 pixels and we&#8217;re gonna identify it with the &#8220;gradientCanvas&#8221; id):<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;html&gt;\r\n&lt;head&gt;\r\n&lt;title&gt;&lt;\/title&gt;\r\n&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text\/html; charset=utf-8&quot;\/&gt; \r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n&lt;div id=&quot;main-content&quot;&gt;\r\n\t&lt;div class=&quot;canvas_button&quot;&gt;\r\n\t\t&lt;a href=&quot;#&quot;&gt;\r\n\t\t&lt;canvas  width=&quot;100&quot; height=&quot;50&quot; id=&quot;gradientCanvas&quot;&gt;\r\n\t\t\tYour browser does not support HTML5 Canvas.\r\n\t\t&lt;\/canvas&gt;\r\n\t\t&lt;\/a&gt;\r\n\t&lt;\/div&gt;\r\n&lt;\/div&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<p>Next step is to create the <strong>drawCanvas1<\/strong>() Javascript function, which will draw inside de canvas element an up-down gradient starting with blue and ending with white:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\nfunction drawCanvas1()\r\n{\r\n\tvar gradientCanvas = document.getElementById('gradientCanvas');\r\n\tvar context = gradientCanvas.getContext('2d');\r\n\tvar myGradient = context.createLinearGradient(0, 0, 0, 60);\r\n\tmyGradient.addColorStop(0, 'blue');\r\n\tmyGradient.addColorStop(1, 'white');\r\n\tcontext.fillStyle = myGradient;\r\n\tcontext.fillRect(0, 0, 100, 50);\r\n}\r\n<\/pre>\n<p>We use the <strong>getContext<\/strong>() method, which returns an object that exposes an API for drawing on the canvas. The API used in this example is &#8216;2d&#8217;.<br \/>\nThe <strong>createLinearGradient<\/strong>() method allows us to set the direction of gradient object(named here myGradient) by specifying the x,y coordinates of start point of the gradient and the end point.<br \/>\nBecause we use white as end color, we set the ending y coordinate to 60(greater than the height of canvas &#8211; i.e. 50 pixels) in order to clearly see the edge of the canvas<br \/>\nThe <strong>addColorStop<\/strong>() method is used to set the colors of the gradient. First argument can take float values from 0.0 to 1.0, which represents the position between the start and end points of the gradient.<\/p>\n<p>We call the function when the document is loaded:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;body onload=&quot;drawCanvas1();&quot;&gt;\r\n<\/pre>\n<p>And now we should see something like this:<\/p>\n<p><img loading=\"lazy\" src=\"https:\/\/www.osd.net\/blog\/wp-content\/uploads\/2011\/05\/1.png\" alt=\"button_canvas_gradient_without_text\" title=\"button_canvas_gradient_without_text\" width=\"100\" height=\"50\" class=\"alignnone size-full wp-image-319\" \/><\/p>\n<p>To add the hover <strong>CSS<\/strong>-like effect, we create a second function, called <strong>drawCanvas2<\/strong>(), similar to previous function:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\nfunction drawCanvas2() \r\n{\t\r\n\t\r\n\tvar gradientCanvas = document.getElementById('gradientCanvas');\r\n\tvar context = gradientCanvas.getContext('2d');\r\n\tvar myGradient = context.createLinearGradient(0, -10, 0, 60);\r\n\tmyGradient.addColorStop(0, 'lightgray');\r\n\tmyGradient.addColorStop(0.5, 'blue');\r\n\tmyGradient.addColorStop(1, 'white');\r\n\tcontext.fillStyle = myGradient;\r\n\tcontext.fillRect(0, 0, 100, 50);\r\n}\r\n<\/pre>\n<p>and set the <strong>onmouseover=&#8221;drawCanvas2<\/strong>()&#8221; and <strong>onmouseout=&#8221;drawCanvas1<\/strong>()&#8221; events for the canvas:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;canvas  onmouseover=&quot;drawCanvas2()&quot; onmouseout=&quot;drawCanvas1()&quot; width=&quot;100&quot; height=&quot;50&quot; id=&quot;gradientCanvas&quot;&gt;\r\n<\/pre>\n<p>Now we should have a simple button created with the <strong>HTML5 canvas<\/strong> element.<br \/>\nTo add text we simply add the following lines at the and of both drawCanvas1() and drawCanvas2() functions, which sets the color for the text fill(line 1), the vertical alignment of the text(line 2), font style(line 3) and finally draws the text, starting from x,y coordinates specified as 2nd and 3rd arguments(line 4):<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\ncontext.fillStyle    = 'white';\r\ncontext.textBaseline = 'top';\r\ncontext.font         = 'bold 30px sans-serif';\r\ncontext.fillText('Home', 10, 10);\r\n<\/pre>\n<p>We should now have a simple button created with canvas element:<\/p>\n<p><img loading=\"lazy\" src=\"https:\/\/www.osd.net\/blog\/wp-content\/uploads\/2011\/05\/2.png\" alt=\"button_canvas_gradient_with_text\" title=\"button_canvas_gradient_with_text\" width=\"100\" height=\"50\" class=\"alignnone size-full wp-image-320\" \/><\/p>\n<p>Hope you&#8217;ll enjoy this short introduction. Feel free to comment.<\/p>\n<p>The complete html file containing both javascript and a bit of css is available <a href='https:\/\/www.osd.net\/blog\/wp-content\/uploads\/2011\/05\/OSD-canvas-button.zip'>here<\/a>.<\/p>\n<div id=\"share-and-beer-container\">\t<div id=\"buy_me_a_beer_div\" class=\"single beer\">\n\t \t\n\t\t<div class=\"buy-beer\" onclick=\"document.getElementById('buy_me_a_beer_form').submit();\">\n\t\t\t<form action=\"https:\/\/www.paypal.com\/cgi-bin\/webscr\" id=\"buy_me_a_beer_form\" method=\"post\">\n\t\t\t\t<input type=\"hidden\" name=\"cmd\" value=\"_xclick\" \/>\n\t\t\t\t<input type=\"hidden\" name=\"business\" value=\"info@directaccess.ro\" \/>  \n\t\t\t\t<input type=\"hidden\" name=\"item_name\" value=\"A Beer For Create a CSS-like button with HMTL5 canvas element using gradient\" \/>  \n\t\t\t\t<input type=\"hidden\" name=\"item_number\" value=\"1\" \/>  \n\t\t\t\t<input type=\"hidden\" name=\"return\" value=\"https:\/\/www.osd.net\/blog\/web-development\/css\/create-a-css-like-button-with-hmtl5-canvas-element-using-gradient\/\" \/>  \n\t\t\t\t<input type=\"hidden\" name=\"amount\" value=\"5\" \/>  \n\t\t\t\t<input type=\"hidden\" name=\"undefined_quantity\" value=\"1\" \/>  \n\t\t\t\t<input type=\"hidden\" name=\"currency_code\" value=\"USD\" \/>  \n\t\t\t<\/form>\n\t\t\t<p class=\"buy-beer-text\">If you liked this post <br \/> you can <strong>buy me a beer<\/strong><\/p>\n\t\t<\/div>\n\t<\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>Canvas is maybe one of the most interesting HTML5 feature. It has all you need to create rich web applications or games. We talked about how to Generate gradient image with PHP some time ago. Now we&#8217;re gonna use HMTL5 canvas element to draw a simple button with a gradient effect, using it&#8217;s 2D context &hellip;<\/p>\n<div class=\"cta1\"><a href=\"https:\/\/www.osd.net\/blog\/web-development\/css\/create-a-css-like-button-with-hmtl5-canvas-element-using-gradient\/\">Read more<\/a><\/div>\n<div class=\"like-excerpt\"><div\n        class=\"fb-like\"\n        data-href=\"https:\/\/www.osd.net\/blog\/web-development\/css\/create-a-css-like-button-with-hmtl5-canvas-element-using-gradient\/\"\n        data-layout=\"button_count\"\n        data-action=\"like\"\n        data-show-faces=\"false\"\n        data-share=\"false\">\n        <\/div><\/div>","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[15,16,18],"tags":[82,84,81,78,83],"_links":{"self":[{"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/posts\/309"}],"collection":[{"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/comments?post=309"}],"version-history":[{"count":11,"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/posts\/309\/revisions"}],"predecessor-version":[{"id":311,"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/posts\/309\/revisions\/311"}],"wp:attachment":[{"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/media?parent=309"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/categories?post=309"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/tags?post=309"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}