{"id":317,"date":"2012-10-09T15:30:39","date_gmt":"2012-10-09T16:30:39","guid":{"rendered":"http:\/\/www.tamats.com\/blog\/?p=317"},"modified":"2012-11-05T18:12:40","modified_gmt":"2012-11-05T19:12:40","slug":"web-experiment-simple-coding","status":"publish","type":"post","link":"https:\/\/www.tamats.com\/blog\/?p=317","title":{"rendered":"Web Experiment: Simple Canvas"},"content":{"rendered":"<p>Today I&#8217;m going to talk about a tiny web app I coded the last week to create images and animations programatically, it is called Simplecanvas.<\/p>\n<figure id=\"attachment_318\" aria-describedby=\"caption-attachment-318\" style=\"width: 500px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/tamats.com\/apps\/simplecanvas\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-318 \" title=\"SImplecoding\" src=\"http:\/\/www.tamats.com\/blog\/wp-content\/uploads\/2012\/10\/preview.png\" alt=\"\" width=\"500\" height=\"300\" \/><\/a><figcaption id=\"caption-attachment-318\" class=\"wp-caption-text\">Click the image to try it<\/figcaption><\/figure>\n<p>The idea is to create images using simple functions like move or drawImage, and test the results instantly by pressing control+enter.<\/p>\n<p><!--more--><\/p>\n<h3>Introduction<\/h3>\n<p>As a graphics programmer and pseudo-artist I&#8217;m always ashamed that if somebody ask me to code some kind of procedural image using simple shapes and algorithms, although I have the knowledge, I wouldnt have the tools to do it right away.<\/p>\n<p>I can create an OpenGL application from scratch but to do a nice image it I will have to program first\u00c2\u00a0 the framework with some useful functions like drawing circles, loading images and so. I have coded plenty of frameworks with different parts of that idea but none of them will fit for this specific purpose.<\/p>\n<p>This kind of frameworks already exists (Processing for Java, openFrameworks or Cinder for C++) but I wanted something I could use right away without having to install anything.<\/p>\n<p>Thats why I started thinking about creating my own little framework for fast coding graphics applications using Javascript and the Canvas element so it can be used right away just entering the site.<\/p>\n<h3>How does it works<\/h3>\n<p>You can code your image or animation similar to what I did in previous web experiments like <a href=\"http:\/\/www.tamats.com\/blog\/?p=226\">hotcoding<\/a> or <a href=\"http:\/\/www.tamats.com\/blog\/?p=250\">mathnimatics<\/a>.You code and you press control+enter to execute. There are lots of webs where you can code on the web and test it clicking a button, like <a href=\"http:\/\/jsfiddle.net\/\">jsfiddle<\/a>, or <a href=\"http:\/\/webglplayground.net\">webglplayground<\/a>, even you can share your creations with the rest of the users.<\/p>\n<p>But I wanted to make it simplier and more compact. I wanted to make it so simple that I could use to teach programming to people how doesnt know where to start.<\/p>\n<p>This are some of the features:<\/p>\n<ul>\n<li>Full set of basic drawing functions, they wrap the regular canvas context functions, but if you want to use the context directly still you can.<\/li>\n<li>Random number generators (seeded, smoothed).<\/li>\n<li>Safe error handling (they check the values to ensure everything is right).<\/li>\n<li>Simplifies most of the annoying actions, like loading and image, reading a pixel of an image, defining colors, etc.<\/li>\n<li>Some basic hooks for events like mouse event and animation<\/li>\n<\/ul>\n<p>After a while I saw that it was powerful enough to creating beautiful images, but the images where stuck in a 1000&#215;500 pixels resolution, so I added a Highresolution function that will render your creation to a much bigger canvas scaling it to fit on it.<\/p>\n<p>Here is an example of a generation:<\/p>\n<p style=\"text-align: center;\"><a href=\"http:\/\/www.tamats.com\/blog\/wp-content\/uploads\/2012\/10\/generator.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter  wp-image-320\" title=\"Programatically created image\" src=\"http:\/\/www.tamats.com\/blog\/wp-content\/uploads\/2012\/10\/generator.png\" alt=\"\" width=\"600\" height=\"300\" \/><\/a><\/p>\n<p>And here is the code to generate it:<\/p>\n<pre lang=\"javascript\">snapshot();\r\nclear(0,0,0.1);\r\n\r\ncolor(1);\r\npush();\r\nscale(3)\r\nsphere(4);\r\nlineWidth(4); \/\/line width also affects arcs\r\nfor(var i = 0; i < 10; i++)\r\n{\r\n\trotate(T*10);\r\n\tcolor(rand(),0.1,0.2);\r\n\tarc(8+i*5,180); \/\/arc example\r\n\tcolor(rand())\r\n\tarc(8+i*5,190,181); \/\/offset example\r\n}\r\npop();\r\nscale(-1.1,-1.1);\r\nrotate(90);\r\ndrawSnapshot(0.4);<\/pre>\n<h3>Future Work<\/h3>\n<p>I can't think of more improvements to the framework provided, but I think I can enhance the creativity by giving the user tools to interact with the canvas in a visual style. For instance, allowing to set points on the canvas by using the mouse, and retrieving those values using simple variables.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today I&#8217;m going to talk about a tiny web app I coded the last week to create images and animations programatically, it is called Simplecanvas. The idea is to create images using simple functions like move or drawImage, and test the results instantly by pressing control+enter.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20,3,22,29,26,21],"tags":[],"class_list":["post-317","post","type-post","status-publish","format-standard","hentry","category-coding","category-devlog","category-graphics","category-html5","category-javascript","category-webdevelopment"],"_links":{"self":[{"href":"https:\/\/www.tamats.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/317","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tamats.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tamats.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tamats.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tamats.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=317"}],"version-history":[{"count":9,"href":"https:\/\/www.tamats.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/317\/revisions"}],"predecessor-version":[{"id":361,"href":"https:\/\/www.tamats.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/317\/revisions\/361"}],"wp:attachment":[{"href":"https:\/\/www.tamats.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=317"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tamats.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=317"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tamats.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=317"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}