Advertisement
Guest User

Untitled

a guest
Jun 29th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /** 1. Draw the following graphics using canvas (Picture1.png) **/
  2. ;(function() {
  3.   var doc = document,
  4.       body = doc.body,
  5.       canvas = doc.createElement('canvas'),
  6.       context = canvas.getContext('2d'),
  7.       PI = Math.PI,
  8.       centerX = 0,
  9.       centerY = 0;
  10.  
  11.   canvas.width = 600;
  12.   canvas.height = 400;
  13.  
  14.   body.appendChild(canvas);
  15.  
  16.   /** draw face **/  
  17.  
  18.   context.save();
  19.   context.translate(150, 245);
  20.   context.beginPath();
  21.   context.arc(centerX, centerY, 80, 0, 2 * PI, false);
  22.   context.restore();
  23.   context.lineWidth = 5;
  24.   context.strokeStyle = '#184a54';
  25.   context.fillStyle = '#90cad7';
  26.   context.stroke();
  27.   context.fill();
  28.   context.closePath();
  29.  
  30.   /** end draw face **/
  31.  
  32.   /** draw eyes **/
  33.  
  34.   context.save();
  35.   context.translate(100, 230);
  36.   context.scale(1.5, 0.8);
  37.   context.beginPath();
  38.   context.arc(centerX, centerY, 12, 0, 2 * PI, false);
  39.   context.closePath();
  40.   context.restore();
  41.   context.lineWidth = 5;
  42.   context.strokeStyle = '#184a54';
  43.   context.fillStyle = '#90cad7';
  44.   context.stroke();
  45.   context.fill();
  46.   context.closePath();
  47.  
  48.   context.save();
  49.   context.translate(160, 230);
  50.   context.scale(1.5, 0.8);
  51.   context.beginPath();
  52.   context.arc(centerX, centerY, 12, 0, 2 * PI, false);
  53.   context.closePath();
  54.   context.restore();
  55.   context.lineWidth = 5;
  56.   context.strokeStyle = '#184a54';
  57.   context.fillStyle = '#90cad7';
  58.   context.stroke();
  59.   context.fill();
  60.   context.closePath();
  61.  
  62.   context.save();
  63.   context.beginPath();
  64.   context.translate(95, 230);
  65.   context.rotate(1.52);
  66.   context.scale(1, 0.5);    
  67.   context.arc(centerX, centerY, 7, 0, 2 * PI, false);
  68.   context.closePath();
  69.   context.restore();
  70.   context.lineWidth = 5;
  71.   context.strokeStyle = '#184a54';
  72.   context.fillStyle = '#184a54';
  73.   context.stroke();
  74.   context.fill();
  75.   context.closePath();
  76.  
  77.   context.save();
  78.   context.beginPath();
  79.   context.translate(155, 230);
  80.   context.rotate(1.52);
  81.   context.scale(1, 0.5);  
  82.   context.arc(centerX, centerY, 7, 0, 2 * PI, false);
  83.   context.closePath();
  84.   context.restore();
  85.   context.lineWidth = 5;
  86.   context.strokeStyle = '#184a54';
  87.   context.fillStyle = '#184a54';
  88.   context.stroke();
  89.   context.fill();
  90.   context.closePath();
  91.  
  92.   /** end draw eyes **/
  93.  
  94.   /** draw nose **/
  95.  
  96.   context.save();
  97.   context.beginPath();
  98.   context.moveTo(130, 230);
  99.   context.lineTo(110, 275);  
  100.   context.restore();
  101.   context.lineWidth = 3;
  102.   context.strokeStyle = '#184a54';
  103.   context.stroke();
  104.   context.closePath();
  105.  
  106.   context.save();
  107.   context.beginPath();
  108.   context.moveTo(111, 276);
  109.   context.lineTo(130, 276);  
  110.   context.restore();
  111.   context.lineWidth = 3;
  112.   context.strokeStyle = '#184a54';
  113.   context.stroke();
  114.   context.closePath();
  115.  
  116.   /** end draw nose **/
  117.  
  118.   /** draw mouth **/
  119.  
  120.   context.save();
  121.   context.beginPath();
  122.   context.translate(135, 300);
  123.   context.rotate(.22);
  124.   context.scale(1, 0.3);  
  125.   context.arc(centerX, centerY, 30, 0, 2 * PI, false);
  126.   context.closePath();
  127.   context.restore();
  128.   context.lineWidth = 5;
  129.   context.strokeStyle = '#184a54';
  130.   context.fillStyle = '#90cad7';
  131.   context.stroke();
  132.   context.fill();
  133.   context.closePath();
  134.  
  135.   /** end draw mouth **/
  136.  
  137.   /** draw hat cylinder **/
  138.  
  139.   context.save();
  140.   context.translate(150, 185);
  141.   context.scale(1.5, 0.3);
  142.   context.beginPath();
  143.   context.arc(centerX, centerY, 80, 0, 2 * PI, false);
  144.   context.restore();
  145.   context.lineWidth = 5;
  146.   context.strokeStyle = 'black';
  147.   context.fillStyle = '#396693';
  148.   context.stroke();
  149.   context.fill();
  150.   context.closePath();
  151.  
  152.   context.beginPath();
  153.   context.save();
  154.   context.moveTo(90, 50);
  155.   context.lineTo(90, 150);
  156.   context.translate(90, 165);
  157.   context.scale(1.5, 0.5);
  158.   context.arc(40, 0, 40, PI, 0, true);
  159.   context.resetTransform();
  160.   context.lineTo(210, 50);
  161.   context.restore();
  162.   context.closePath();
  163.   context.lineWidth = 5;
  164.   context.strokeStyle = 'black';
  165.   context.fillStyle = '#396693';
  166.   context.stroke();
  167.   context.fill();
  168.  
  169.   context.save();
  170.   context.translate(150, 50);
  171.   context.scale(1.5, 0.5);
  172.   context.beginPath();
  173.   context.arc(centerX, centerY, 40, 0, 2 * PI, false);
  174.   context.restore();
  175.   context.lineWidth = 5;
  176.   context.strokeStyle = 'black';
  177.   context.fillStyle = '#396693';
  178.   context.stroke();
  179.   context.fill();
  180.   context.closePath();
  181.  
  182.   /** end draw hat cylinder **/
  183.  
  184. }());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement