{"id":1410,"date":"2015-01-21T10:34:11","date_gmt":"2015-01-21T09:34:11","guid":{"rendered":"http:\/\/www.aymericlamboley.fr\/blog\/?p=1410"},"modified":"2015-01-21T10:58:39","modified_gmt":"2015-01-21T09:58:39","slug":"dargaud-cartography-pure-javascript-isnt-so-bad","status":"publish","type":"post","link":"http:\/\/www.aymericlamboley.fr\/blog\/dargaud-cartography-pure-javascript-isnt-so-bad\/","title":{"rendered":"Dargaud cartography, pure JavaScript isn&#8217;t so bad&#8230;"},"content":{"rendered":"<p>New year comes with plenty new resolutions, one of them was to finally give a serious try to a small project into pure JavaScript. I used to swear on JavaScript every time it was mentionned. I made several projects using <a href=\"http:\/\/typescriptlang.org\/\" target=\"_blank\">TypeScript<\/a> and <a href=\"http:\/\/haxe.org\/\" target=\"_blank\">Haxe<\/a>, and I&#8217;m very happy with those techs. But, if there is some strong libraries (<a href=\"http:\/\/pixijs.com\/\" target=\"_blank\">Pixi.js<\/a>, <a href=\"http:\/\/phaser.io\/\" target=\"_blank\">Phaser<\/a>, <a href=\"http:\/\/threejs.org\/\" target=\"_blank\">three.js<\/a>) made in pure JavaScript, there are certainly some reasons !? And when you know that those guys are all ex-flashers, you think that they would use a compiled language. But not at all, they don&#8217;t want to depend on a company (since Adobe&#8217;s failure on Flash&#8230;), freedom feeling you said?<\/p>\n<p><!--more--><\/p>\n<p><strong>Dargaud&#8217;s cartography<\/strong><br \/>\n<a href=\"http:\/\/www.dargaud.com\/cartographie-2015\/\"><img decoding=\"async\" loading=\"lazy\" src=\"http:\/\/www.aymericlamboley.fr\/blog\/wp-content\/uploads\/2015\/01\/cartography-300x182.jpg\" alt=\"cartography\" width=\"300\" height=\"182\" class=\"alignleft size-medium wp-image-1417\" srcset=\"http:\/\/www.aymericlamboley.fr\/blog\/wp-content\/uploads\/2015\/01\/cartography-300x182.jpg 300w, http:\/\/www.aymericlamboley.fr\/blog\/wp-content\/uploads\/2015\/01\/cartography-1024x621.jpg 1024w, http:\/\/www.aymericlamboley.fr\/blog\/wp-content\/uploads\/2015\/01\/cartography.jpg 1531w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><a href=\"http:\/\/en.wikipedia.org\/wiki\/Dargaud\" target=\"_blank\">Dargaud<\/a> is a famous publisher of Franco-Belgian comics series. In this greeting card <a href=\"http:\/\/www.dargaud.com\/cartographie-2015\/\" target=\"_blank\">project<\/a>, they introduce their comic strip classified by themes simulating a constellation.<\/p>\n<p>The project was made using <a href=\"http:\/\/pixijs.com\/\" target=\"_blank\">Pixi.js<\/a>, GreenSock&#8217;s <a href=\"http:\/\/greensock.com\/\" target=\"_blank\">TweenMax<\/a> library and <a href=\"http:\/\/millermedeiros.github.io\/js-signals\/\" target=\"_blank\">Signals<\/a>. I felt right at home!<\/p>\n<p>Now let&#8217;s dig into some JavaScript code.<\/p>\n<p><strong>OOP with JavaScript<\/strong><\/p>\n<pre lang=\"javascript\">\/\/ Define the Person constructor\r\nfunction Person (firstName) {\r\n\r\n  \/\/public properties:\r\n  this.firstName = firstName;\r\n  this.myAge = 2;\r\n};\r\n\r\n\/\/ Add a couple of public methods to Person.prototype\r\nPerson.prototype.walk = function() {\r\n  console.log(\"I am walking!\");\r\n};\r\n\r\nPerson.prototype.sayHello = function() {\r\n  console.log(\"Hello, I'm \" + this.firstName);\r\n};\r\n\r\n\/\/ getter & setter\r\nObject.defineProperty(Person.prototype, 'age', {\r\n\r\n\tget: function() {\r\n\t\treturn 12;\r\n\t},\r\n\tset: function(value) {\r\n\t\tthis.myAge = value;\r\n\t}\r\n});\r\n\r\n\r\n\/\/ Define the Student constructor\r\nfunction Student(firstName, subject) {\r\n\r\n  \/\/ Call the parent constructor, making sure that \"this\" is set correctly during the call\r\n  Person.call(this, firstName);\r\n\r\n  \/\/ Initialize our Student-specific properties\r\n  this.subject = subject;\r\n};\r\n\r\n\/\/ Create a Student.prototype object that inherits from Person.prototype.\r\nStudent.prototype = Object.create(Person.prototype);\r\n\r\n\/\/ Set the \"constructor\" property to refer to Student\r\nStudent.prototype.constructor = Student;\r\n\r\n\/\/ Replace the \"sayHello\" method\r\nStudent.prototype.sayHello = function() {\r\n\r\n  \/\/ you may call the parent \"super\":\r\n  Person.prototype.sayHello.call(this);\r\n\r\n  console.log(\"Hello, I'm \" + this.firstName + \". I'm studying \" + this.subject + \".\");\r\n};\r\n\r\nStudent.prototype.sayGoodBye = function() {\r\n  console.log(\"Goodbye!\");\r\n};\r\n\r\n\/\/static method\r\nStudent.goingToSchool = function() {\r\n  console.log(\"student enjoy going to school.\")\r\n}<\/pre>\n<pre lang=\"javascript\">var student1 = new Student(\"Janet\", \"Applied Physics\");\r\nstudent1.sayHello();\r\nstudent1.walk();\r\nstudent1.sayGoodBye();\r\n\r\nconsole.log(student1.age);\r\n\r\nStudent.goingToSchool();\r\n\r\nconsole.log(student1 instanceof Person);\r\nconsole.log(student1 instanceof Student);<\/pre>\n<p>In this small sample we can see that JavaScript has already everything needed on a daily basis.<\/p>\n<p>Autocompletion you said? Well, with recent IDEs you shouldn&#8217;t have any issues. For this small project, I give a try to <a href=\"http:\/\/netbeans.org\/\" target=\"_blank\">NetBeans<\/a>, a free IDE. I was ok with its features, but how may a simple project with 5 files in html\/js eat so much memory?<br \/>\n<a href=\"http:\/\/www.aymericlamboley.fr\/blog\/wp-content\/uploads\/2015\/01\/netbeans.png\"><img decoding=\"async\" loading=\"lazy\" src=\"http:\/\/www.aymericlamboley.fr\/blog\/wp-content\/uploads\/2015\/01\/netbeans-300x53.png\" alt=\"netbeans\" width=\"300\" height=\"53\" class=\"alignleft size-medium wp-image-1421\" srcset=\"http:\/\/www.aymericlamboley.fr\/blog\/wp-content\/uploads\/2015\/01\/netbeans-300x53.png 300w, http:\/\/www.aymericlamboley.fr\/blog\/wp-content\/uploads\/2015\/01\/netbeans.png 800w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a> That&#8217;s crazy! The next time, I will stay with <a href=\"https:\/\/www.jetbrains.com\/webstorm\/\" target=\"_blank\">WebStorm<\/a>.<\/p>\n<p>You probably noticed the smooth object rotation on the ellipse. Here is the formula:<\/p>\n<pre lang=\"javascript\">var centerX = 400;\r\nvar centerY = 200;\r\n\r\nvar radiusWidth = 400;\r\nvar radiusHeight = 150;\r\n\r\nvar ellipse = new PIXI.Graphics();\r\nellipse.lineStyle(1, 0xFFFFFF);\r\nellipse.drawEllipse(centerX, centerY, radiusWidth, radiusHeight);\r\nstage.addChild(ellipse);\r\n\r\nvar planet = new PIXI.Graphics();\r\nplanet.beginFill(0x00FF00);\r\nplanet.drawCircle(0, 0, 50);\r\nstage.addChild(planet);\r\n\r\nvar alpha = 0;\r\n\r\nfunction animate() {\r\n\r\n\trequestAnimFrame(animate);\r\n\r\n\tplanet.x = centerX + (radiusWidth * Math.cos(alpha));\r\n\tplanet.y = centerY + (radiusHeight * Math.sin(alpha));\r\n\r\n\talpha += 0.01;\r\n\r\n\trenderer.render(stage);\r\n}<\/pre>\n<p>Note that it will also works for a simple circle!<\/p>\n<p>This project was also an opportunity to give a try to the remote <a href=\"https:\/\/remote.modern.ie\/\" target=\"_blank\">IE<\/a>. Finally Microsoft gave us a simple way to test on IE and it works nicely!<\/p>\n<p>I didn&#8217;t have strong issues coding with JavaScript, excepted that one: function positions. It seems that you can&#8217;t define a public function in your class before you mentionned your OOP part (<em>Object.create<\/em>) and the constructor. It drives me crazy during one hour, but other than that, no problem sir! Obviously a compiler would remove all those hand written mistakes \ud83d\ude09<\/p>\n<p><strong>Freedom<\/strong><br \/>\nMaking this project in pure JavaScript, I enjoyed the feeling of freedom. If in two years I&#8217;ve to push a simple update, I won&#8217;t have to update (upgrade?) my IDE, download the latest SDK, update to the latest language version (TypeScript you said?) and change other things. I&#8217;ll just need a text editor and make the change while the big browser guys work in the same direction to unify their JavaScript management. And that&#8217;s all what we wish.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>New year comes with plenty new resolutions, one of them was to finally give a serious try to a small project into pure JavaScript. I used to swear on JavaScript every time it was mentionned. I made several projects using TypeScript and Haxe, and I&#8217;m very happy with those techs. But, if there is some &hellip; <a href=\"http:\/\/www.aymericlamboley.fr\/blog\/dargaud-cartography-pure-javascript-isnt-so-bad\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Dargaud cartography, pure JavaScript isn&#8217;t so bad&#8230;<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[180,84,168,6,181],"tags":[86,185],"_links":{"self":[{"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/posts\/1410"}],"collection":[{"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/comments?post=1410"}],"version-history":[{"count":15,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/posts\/1410\/revisions"}],"predecessor-version":[{"id":1428,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/posts\/1410\/revisions\/1428"}],"wp:attachment":[{"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/media?parent=1410"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/categories?post=1410"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/tags?post=1410"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}