{"id":3295,"date":"2021-01-17T22:39:10","date_gmt":"2021-01-18T03:39:10","guid":{"rendered":"http:\/\/bluegalaxy.info\/codewalk\/?p=3295"},"modified":"2021-01-17T22:39:13","modified_gmt":"2021-01-18T03:39:13","slug":"vue-js-how-to-make-an-identicon-generator","status":"publish","type":"post","link":"https:\/\/bluegalaxy.info\/codewalk\/2021\/01\/17\/vue-js-how-to-make-an-identicon-generator\/","title":{"rendered":"Vue.js: How to make an identicon generator"},"content":{"rendered":"\n<p>In this article, I will show how to create an identicon generator using <code>Vue.js<\/code> in codepen. First of all, an identicon is a graphical image that is generated by text input. This is useful as a substitute for photo images for site users.<\/p>\n\n\n\n<p>For example, when I type &#8220;Chris Nielsen Code Walk&#8221;, the identicon generator creates the image seen here:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized is-style-default\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2021\/01\/image.png\" alt=\"\" class=\"wp-image-3298\" width=\"293\" height=\"357\" srcset=\"https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2021\/01\/image.png 675w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2021\/01\/image-246x300.png 246w\" sizes=\"auto, (max-width: 293px) 100vw, 293px\" \/><\/figure>\n\n\n\n<p>The identicon is generated via an external library called &#8220;jdenticon&#8221;. In codepen, we first need to go to the JS settings and pull in two libraries:<\/p>\n\n\n\n<p><a href=\"https:\/\/cdn.rawgit.com\/dmester\/jdenticon\/5bbdf8cb\/dist\/jdenticon.min.js\">https:\/\/cdn.rawgit.com\/dmester\/jdenticon\/5bbdf8cb\/dist\/jdenticon.min.js<\/a><br>and<br><a href=\"https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/vue\/2.6.11\/vue.min.js\">https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/vue\/2.6.11\/vue.min.js<\/a><\/p>\n\n\n\n<p>These can be found by clicking the settings icon next to JS and typing in &#8220;jdenticon&#8221; or &#8220;vue&#8221; and selecting one of the CDN results. For example:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized is-style-default\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2021\/01\/image-1-1024x792.png\" alt=\"\" class=\"wp-image-3299\" width=\"768\" height=\"594\" srcset=\"https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2021\/01\/image-1-1024x792.png 1024w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2021\/01\/image-1-300x232.png 300w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2021\/01\/image-1-768x594.png 768w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2021\/01\/image-1-1536x1188.png 1536w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2021\/01\/image-1-2048x1583.png 2048w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2021\/01\/image-1-676x523.png 676w\" sizes=\"auto, (max-width: 768px) 100vw, 768px\" \/><\/figure>\n\n\n\n<p>Starting with the HTML section in codepen, all we need for this Vue.js app is a single div with the id of the app. For example:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"html\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;div id=\"app\">&lt;\/div><\/pre>\n\n\n\n<p>The rest of the code will be in the JavaScript section. First we need a new Vue constructor:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">new Vue({\n\n});<\/pre>\n\n\n\n<p>Then we need to tell Vue what div in the HTML to target. This can be done with an <code>el: 'target'<\/code>. For example:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">new Vue({\n  el: '#app',\n\n});<\/pre>\n\n\n\n<p>In the Vue constructor, we can set up our data object, methods, computed section, and template. For example:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">new Vue({\n  el: '#app',\n  data: { \/\/ key-value pairs\n    textInput: ''\n  },\n  computed: { \/\/ Turn our data into viewable values with these functions\n    identicon: function() {\n      return jdenticon.toSvg(this.textInput, 200);\n    }\n  },\n  methods: { \/\/ Use these functions to change data\n    onInput: function(event) {\n      \/\/ console.log(event.target.value);\n      this.textInput = event.target.value;\n    }\n  },\n  template: `\n    &lt;div>\n      &lt;h3>Identicon Generator&lt;\/h3>\n      &lt;div>\n        Input: \n        &lt;input v-on:input=\"onInput\"\/>\n        &lt;!-- The above line is a vue \"directive\" -->\n        &lt;!-- v-on = event handler -->\n        &lt;!-- The event is input -->\n        &lt;!-- onInput is the vue method that is called -->\n      &lt;\/div>\n\n      &lt;div>\n        Output: \n        &lt;!-- {{ identicon }} -->\n        &lt;div v-html=\"identicon\">&lt;\/div>\n      &lt;\/div>\n    &lt;\/div>\n  `\n});<\/pre>\n\n\n\n<p>The complete app can be seen in my codepen:<\/p>\n\n\n<p class='codepen'  data-height='400' data-theme-id='0' data-slug-hash='mdrMaxZ' data-default-tab='js,result' data-animations='run' data-editable='' data-embed-version='2'>\nSee the Pen Vue Identicon Generator by Chris Nielsen (@Chris_Nielsen) on CodePen.<\/p>\n\n","protected":false},"excerpt":{"rendered":"<p>In this article, I will show how to create an identicon generator using Vue.js in codepen. First of all, an identicon is a graphical image that is generated by text input. This is useful as a substitute for photo images for site users. For example, when I type &#8220;Chris Nielsen Code Walk&#8221;, the identicon generator &hellip; <a href=\"https:\/\/bluegalaxy.info\/codewalk\/2021\/01\/17\/vue-js-how-to-make-an-identicon-generator\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Vue.js: How to make an identicon generator<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[44,221],"tags":[45,61],"class_list":["post-3295","post","type-post","status-publish","format-standard","hentry","category-javascript-language","category-vue-js","tag-javascript","tag-vue-js"],"_links":{"self":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/3295","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/comments?post=3295"}],"version-history":[{"count":11,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/3295\/revisions"}],"predecessor-version":[{"id":3380,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/3295\/revisions\/3380"}],"wp:attachment":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/media?parent=3295"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/categories?post=3295"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/tags?post=3295"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}