BriocheWindows

Random Colour Character Maker

Apr 10th, 2022
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <html>
  2.  
  3. <head>
  4.  
  5.     <title>Avatar Maker</title>
  6.  
  7. <script>
  8.  
  9. window.onload = function ()
  10.     {
  11.        //Each block of code here from the head down to the background generates a different random component of the character.
  12.         //head
  13.         var robothead = new Image();
  14.         var robotheadnum=Math.floor(Math.random()*6)+1; //Change "3" to whatever number of head files you have
  15.         var robotheadname="head" + robotheadnum + ".png";
  16.         robothead.src=robotheadname;
  17.  
  18.         //eyes
  19.         var roboteyes = new Image();
  20.         var roboteyesnum=Math.floor(Math.random()*4)+1; //Change "3" to whatever number of head files you have
  21.         var roboteyesname="eyes" + roboteyesnum + ".png";
  22.         roboteyes.src=roboteyesname;
  23.  
  24.          //mouth
  25.          var robotmouth = new Image();
  26.         var robotmouthnum=Math.floor(Math.random()*6)+1; //Change "3" to whatever number of head files you have
  27.         var robotmouthname="mouth" + robotmouthnum + ".png";
  28.         robotmouth.src=robotmouthname;
  29.  
  30.         //body
  31.         var robotbody = new Image();
  32.         var robotbodynum=Math.floor(Math.random()*4)+1; //Change "3" to whatever number of head files you have
  33.         var robotbodyname="body" + robotbodynum + ".png";
  34.         robotbody.src=robotbodyname;
  35.  
  36.         //background
  37.         var robotbackground = new Image();
  38.         var robotbackgroundnum=Math.floor(Math.random()*36)+1; //Change "3" to whatever number of head files you have
  39.         var robotbackgroundname="background" + robotbackgroundnum + ".png";
  40.         robotbackground.src=robotbackgroundname;
  41.  
  42.         // hue overlay
  43.         // This part of the code generates a random hue for the character. This is applied below in the "canvas" portion of the code.
  44.         var robothue = new Image();
  45.         var robothuenum=Math.floor(Math.random()*359)+1;
  46.  
  47.         //head loaded
  48.         robothead.onload=function()
  49.         {
  50.             buildrobot();
  51.         }
  52.  
  53.         //eyes loaded
  54.         roboteyes.onload=function()
  55.         {
  56.             buildrobot();
  57.         }
  58.        
  59.         //mouth loaded
  60.          robotmouth.onload=function()
  61.         {
  62.             buildrobot();
  63.         }
  64.  
  65.         //body loaded
  66.         robotbody.onload=function()
  67.         {
  68.             buildrobot();
  69.         }
  70.  
  71.         //body loaded
  72.         robotbackground.onload=function()
  73.         {
  74.             buildrobot();
  75.         }
  76.  
  77.         //hue loaded
  78.         robothue.onload=function()
  79.         {
  80.             buildrobot();
  81.         }
  82.  
  83.         function buildrobot()
  84.         {
  85.             var canvas=document.getElementById('canvas');
  86.             var ctx = canvas.getContext('2d');
  87.             canvas.width=1120;
  88.             canvas.height=1120;
  89.             canvas.style.filter = 'hue-rotate(' + robothuenum + 'deg)'; //This line applies the hue filter mentioned above.
  90.  
  91.             //draw body
  92.             ctx.drawImage(robotbackground,((1120-robotbody.width)/2),0);
  93.             //draw body
  94.             ctx.drawImage(robotbody,((1120-robotbody.width)/2),0);
  95.             //draw eyes
  96.             ctx.drawImage(robothead,((1120-robothead.width)/2),0);
  97.             //draw mouth
  98.             ctx.drawImage(robotmouth,((1120-robotmouth.width)/2),0);
  99.             //draw head
  100.             ctx.drawImage(roboteyes,((1120-roboteyes.width)/2),0);
  101.            
  102.         }
  103.     }
  104.  
  105. </script>
  106.  
  107. </head>
  108.  
  109. <body>
  110.  
  111.     <div align="center">
  112.         <canvas id="canvas" style="border:5px solid #000000;"></canvas>
  113.         <br><br>
  114.         <button onClick="window.location.reload();"> Generate! </button>
  115.     </div>
  116.  
  117. </body>
  118.  
  119. </html>
Advertisement
Add Comment
Please, Sign In to add comment