{"id":1280,"date":"2014-01-07T18:30:03","date_gmt":"2014-01-07T18:30:03","guid":{"rendered":"http:\/\/johnjohnston.info\/106\/?p=1280"},"modified":"2014-01-08T22:52:04","modified_gmt":"2014-01-08T22:52:04","slug":"cubomatic","status":"publish","type":"post","link":"https:\/\/johnjohnston.info\/106\/cubomatic\/","title":{"rendered":"Cubomatic"},"content":{"rendered":"<p><a href=\"http:\/\/johnjohnston.info\/106\/wp-content\/uploads\/2014\/01\/fr_960.jpg\"><img decoding=\"async\" class=\"aligncenter size-large wp-image-1281\" alt=\"fr_960\" src=\"https:\/\/johnjohnston.info\/106\/wp-content\/uploads\/2014\/01\/fr_960-1024x768.jpg\" width=\"600\" srcset=\"https:\/\/johnjohnston.info\/106\/wp-content\/uploads\/2014\/01\/fr_960-1024x768.jpg 1024w, https:\/\/johnjohnston.info\/106\/wp-content\/uploads\/2014\/01\/fr_960-300x225.jpg 300w, https:\/\/johnjohnston.info\/106\/wp-content\/uploads\/2014\/01\/fr_960.jpg 1280w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/a><\/p>\n<p><a href=\"http:\/\/johnjohnston.info\/oddsandends\/Cubomania.html\">Cubomania!<\/a><\/p>\n<p>Yesterday&#8217;s <a href=\"http:\/\/tdc.ds106.us\/tdc729\" title=\"Create a Self Portrait Cubomania Style\">daily create<\/a> was an interesting one:<\/p>\n<blockquote><p>Create a Self Portrait Cubomania Style<\/p>\n<p>Cubomania is a surrealist method of making collages in which a picture or image is cut into squares and the squares are then reassembled without regard for the image<br \/>\u2013 <a href=\"http:\/\/en.wikipedia.org\/wiki\/Cubomania\">http:\/\/en.wikipedia.org\/wiki\/Cubomania<\/a><\/p>\n<\/blockquote>\n<p>One of the nice things about the daily create is the new words you learn.<br \/>\nI though it might be more interesting to do this with code  rather than slicing and dicing an image. Turned out I was only half right on this, as other folk turned in <a href=\"http:\/\/www.flickr.com\/photos\/tags\/tdc729\">more interesting photos<\/a>, but my process was interesting to me at least.<br \/>\nMy first thoughts were to use SuperCard on my mac, but I then though it would be fun to try to do this on the web. I&#8217;ve done a little image manipulation online before, first I though of creating a form to upload an image to a server and use php to edit it with gd. (I did this for <a href=\"http:\/\/johnjohnston.info\/flickrCC\/\">A flickr CC<\/a>). I also thought about using javascript on a canvas element which lead me to wonder if you could do this without images reaching the server. A bit of googling and <a href=\"http:\/\/www.html5rocks.com\/en\/tutorials\/file\/dndfiles\/\">Reading local files in JavaScript &#8211; HTML5 Rocks<\/a> pretty much fitted the bill, letting me cut and paste a chunk of code to pull an image into a page.<br \/>\nOnce the image was on the page I could get to work on it. I am not sure if it is worth going over the code as it is pretty horrible, but if you look at the <a href=\"http:\/\/ole.michelsen.dk\/viewsource\/?uri=http:\/\/johnjohnston.info\/oddsandends\/Cubomania.html\">source<\/a> the manipulation is in the cubomania function.<br \/>\nFirst find out the image dimensions and set the canvas element to the correct size.<\/p>\n<pre><code>var w=document.getElementById('newimage').width;\r\nvar h= document.getElementById('newimage').height;\r\ndocument.getElementById('myCanvas').height=h;\r\ndocument.getElementById('myCanvas').width=w;<\/code><\/pre>\n<p>It is then simple to work out the width and height of each square (one tenth of the width and height) and then an array of the coordinates of the top left of each cube.<br \/>\nThis array is duplicated and one of the two arrays is randomly shuffled.<br \/>\nI discovered that copying the array and then shuffling one did not work, I needed to use <code>otiles=tiles.slice(0);<\/code> to get a copy that would not change when I shuffled the original list (tiles).<br \/>\nFor shuffling I again hit google: <a href=\"http:\/\/stackoverflow.com\/questions\/2450954\/how-to-randomize-shuffle-a-javascript-array\">How to randomize (shuffle) a javascript array? &#8211; Stack Overflow<\/a><\/p>\n<p>Once I have two lists it is then a case of drawing each square from one list at the location of the same slot in the second list with the <a href=\"http:\/\/www.w3schools.com\/tags\/canvas_drawimage.asp\">HTML canvas drawImage() Method<\/a>.<\/p>\n<p>The last bit of the puzzle is to take the canvas image and turn it back into a &#8216;real&#8217; image that can be downloaded. I&#8217;ve done that before, so only needed to check <a href=\"http:\/\/johnjohnston.info\/106\/alternative-lines-for-the-daily-create\/\">my old page<\/a>.<br \/>\nOf course I did not get any of this right the first time. My maths is not very good and I needed a few goes to work out how to fill the array mostly by trail and error. It is very messy in there. I also stored each location as an item in the array, this meant I needed to split it when using drawImage. I <s>suspect<\/s> know there are many many better ways to do this.<br \/>\nI am not posting this as an example of good practise, or recommending it as a method, more as an example of how you can have fun with ds106 by twisting the task and that you can have fun with code without really knowing what you are doing. Half the battle is guessing what you can do and then google will help with solutions. I am also aware this is not proper coding and my problem solving methods (guess and check) are far from useful, but I do enjoy myself.<\/p>\n<p>Here is <a href=\"http:\/\/johnjohnston.info\/oddsandends\/Cubomania.html\">Cubomania!<\/a> ready cubing your pictures.<\/p>\n<p><strong>Update 8 January<\/strong> a verson that makes gifs: <a href=\"http:\/\/johnjohnston.info\/oddsandends\/cubomaniagif.html\">Cubomania Gif!<\/a><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/johnjohnston.info\/106\/wp-content\/uploads\/2014\/01\/cubomania.gif\" alt=\"cubomania\" width=\"400\" height=\"300\" class=\"aligncenter size-full wp-image-1290\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Cubomania! Yesterday&#8217;s daily create was an interesting one: Create a Self Portrait Cubomania Style Cubomania is a surrealist method of making collages in which a picture or image is cut into squares and the squares are then reassembled without regard for the image\u2013 http:\/\/en.wikipedia.org\/wiki\/Cubomania One of the nice things about the daily create is the &hellip; <a href=\"https:\/\/johnjohnston.info\/106\/cubomatic\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Cubomatic&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[81],"tags":[77],"class_list":["post-1280","post","type-post","status-publish","format-standard","hentry","category-dailycreate","tag-javascript"],"better_featured_image":null,"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p3RLlC-kE","_links":{"self":[{"href":"https:\/\/johnjohnston.info\/106\/wp-json\/wp\/v2\/posts\/1280","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/johnjohnston.info\/106\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/johnjohnston.info\/106\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/johnjohnston.info\/106\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/johnjohnston.info\/106\/wp-json\/wp\/v2\/comments?post=1280"}],"version-history":[{"count":6,"href":"https:\/\/johnjohnston.info\/106\/wp-json\/wp\/v2\/posts\/1280\/revisions"}],"predecessor-version":[{"id":1291,"href":"https:\/\/johnjohnston.info\/106\/wp-json\/wp\/v2\/posts\/1280\/revisions\/1291"}],"wp:attachment":[{"href":"https:\/\/johnjohnston.info\/106\/wp-json\/wp\/v2\/media?parent=1280"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/johnjohnston.info\/106\/wp-json\/wp\/v2\/categories?post=1280"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/johnjohnston.info\/106\/wp-json\/wp\/v2\/tags?post=1280"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}