Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var colors = {
  2.     red: '#ff0000',
  3.     skyblue: '#99d9ea',
  4.     deepblue: '#6ecaf6',
  5.     darkbrown: '#7a4315',
  6.     brown: '#905721',
  7.     forestgreen: '#257a15',
  8.     green: '#1db500',
  9.     lime: '#00ff00',
  10.     black: '#000000',
  11.     white: '#ffffff',
  12.     gray: '#808080',
  13.     crimson: '#cb0000',
  14.     lightgray: '#959595',
  15. }
  16.  
  17. var settings = {
  18.     canvas: {
  19.         height: 200,
  20.         width: 400,
  21.     },
  22.     windows: {
  23.         color: colors.skyblue
  24.     },
  25.     bricks: {
  26.         height: 13,
  27.         width: 20,
  28.     },
  29.     grass: {
  30.         minHeight: 5,
  31.         maxHeight: 11,
  32.         angle: 30,
  33.         density: 2
  34.     }
  35. }
  36.  
  37. var canvas = document.createElement('canvas');
  38. canvas.setAttribute('height', settings.canvas.height);
  39. canvas.setAttribute('width', settings.canvas.width);
  40. canvas.style.padding = '300px';
  41. document.body.appendChild(canvas);
  42.  
  43. function rand(min, max) { // Generate random int between min and max
  44.     return Math.floor(Math.random() * (max - min + 1)) + min;
  45. }
  46.  
  47. function rad(deg) {
  48.     return (deg / 180) * Math.PI
  49. }
  50.  
  51. function deg(rad) {
  52.     return rad / Math.PI * 180
  53. }
  54.  
  55. function transformY(y, height) {
  56.     return (settings.canvas.height - y) - (height || 0)
  57. }
  58.  
  59. function fill(x, y, width, height, color) {
  60.     y = transformY(y, height)
  61.     var c2d = canvas.getContext("2d");
  62.     c2d.fillStyle = color || colors.black;
  63.     c2d.beginPath();
  64.     c2d.fillRect(x, y, width, height);
  65. }
  66.  
  67. function rect(x, y, width, height, color) {
  68.     y = transformY(y, height)
  69.     var c2d = canvas.getContext("2d");
  70.     c2d.beginPath();
  71.     c2d.strokeStyle = color || colors.black;
  72.     c2d.rect(x, y, width, height);
  73.     c2d.stroke();
  74. }
  75.  
  76. function strokePath(color, lines) {
  77.     var c2d = canvas.getContext("2d");
  78.     c2d.beginPath();
  79.     c2d.strokeStyle = color || colors.black;
  80.     c2d.moveTo(lines[0][0], transformY(lines[0][1]));
  81.     for (var i in lines) {
  82.         if (i == 0) continue;
  83.         c2d.lineTo(lines[i][0], transformY(lines[i][1]));
  84.     }
  85.     c2d.stroke();
  86. }
  87.  
  88. function fillPath(color, lines) {
  89.     var c2d = canvas.getContext("2d");
  90.     c2d.beginPath();
  91.     c2d.fillStyle = color || colors.black;
  92.     c2d.moveTo(lines[0][0], transformY(lines[0][1]));
  93.     for (var i in lines) {
  94.         if (i == 0) continue;
  95.         c2d.lineTo(lines[i][0], transformY(lines[i][1]));
  96.     }
  97.     c2d.fill();
  98. }
  99.  
  100. function fillCircle(x, y, r, color) {
  101.     y = transformY(y)
  102.     var c2d = canvas.getContext("2d");
  103.     c2d.beginPath();
  104.     c2d.fillStyle = color || colors.black;
  105.     c2d.arc(x, y, r, 0, rad(360));
  106.     c2d.fill();
  107. }
  108.  
  109. function strokeCircle(x, y, r, color) {
  110.     y = transformY(y)
  111.     var c2d = canvas.getContext("2d");
  112.     c2d.beginPath();
  113.     c2d.fillStyle = color || colors.black;
  114.     c2d.arc(x, y, r, 0, rad(360));
  115.     c2d.stroke();
  116. }
  117.  
  118. function grass(x, y, length, angle) {
  119.     y = transformY(y)
  120.     var c2d = canvas.getContext("2d");
  121.     c2d.beginPath();
  122.     c2d.strokeStyle = colors.forestgreen;
  123.     c2d.moveTo(x, y);
  124.     c2d.lineTo(x - Math.sin(angle) * length, y - Math.cos(angle) * length);
  125.     c2d.stroke();
  126. }
  127.  
  128. fill(0, 0, 400, 200, colors.deepblue); // Background
  129.  
  130. rect(50, 5, 300, 100); // Body
  131.  
  132. fill(40, 2, 320, 3, colors.gray); // Foundation
  133. rect(0, 0, 400, 2, colors.green); // Grass
  134.  
  135. // Bricks
  136. for (var y = 5; y < 160; y += settings.bricks.height) {
  137.     var offset = (((y - 5) / settings.bricks.height) % 2) // Will be 1 for odds and 0 for evens
  138.                     * (settings.bricks.width / 2); // Offset every other row
  139.  
  140.     var newHeight = Math.min(settings.bricks.height, Math.max(160 - y, 0));
  141.  
  142.     for (var x = 50 - offset; x < 350; x += settings.bricks.width) {
  143.         var brickColor = '#' + Number(rand(60, 120)).toString(16) + '0000'; // Random darkish red
  144.         var startX = Math.max(x, 50); // Take care of the overhang on left
  145.         var newWidth = startX != x ? settings.bricks.width / 2 : startX + settings.bricks.width > 350 ? startX + settings.bricks.width - 350 : settings.bricks.width; // take care of the differently sized bricks
  146.  
  147.         fill(startX, y, newWidth, newHeight, brickColor); // Brick Fill
  148.         rect(startX, y, newWidth, newHeight); // Brick Outline
  149.     }
  150. }
  151.  
  152. /* START ROOF TOP */
  153.     var distance = ((50 - 200) ^ 2 + (100 - 160) ^ 2) ^ (1 / 2);
  154.     var theta = Math.atan(-150 / -60) + Math.PI * 1.5;
  155.  
  156.     var points = [
  157.         [200, 160],
  158.         [(distance + 5) * Math.cos(theta) + 200, (distance + 5) * Math.sin(theta) + 160],
  159.     ];
  160.  
  161.     points.push([10 * Math.cos(rad(68)) + points[1][0], 10 * Math.sin(rad(68)) + points[1][1]]);
  162.     points.push([10 * Math.cos(rad(90)) + points[0][0], 10 * Math.sin(rad(90)) + points[0][1]]);
  163.     points.push([50 - (points[2][0] - 350), points[2][1]]);
  164.     points.push([50 - (points[1][0] - 350), points[1][1]]);
  165.  
  166.     var mask = [
  167.         points[2],
  168.         [settings.canvas.width, points[2][1]],
  169.         [settings.canvas.width, settings.canvas.height],
  170.         [0, settings.canvas.height],
  171.         [0, points[4][1]],
  172.         points[4],
  173.         points[3],
  174.         [(distance - 60) * Math.cos(theta) + points[3][0], (distance - 60) * Math.sin(theta) + points[3][1]]
  175.     ];
  176.  
  177.     // CHIMNEY so much math
  178.     mask.push([mask[7][0], mask[7][1] + 20]);
  179.     mask.push([mask[8][0] - 5, mask[8][1]]);
  180.     mask.push([mask[9][0], mask[9][1] + 5]);
  181.     mask.push([mask[10][0] + 30, mask[10][1]]);
  182.     mask.push([mask[11][0], mask[11][1] - 5]);
  183.     mask.push([mask[12][0] - 5, mask[12][1]]);
  184.  
  185.     var maskDistance = (20 / Math.cos(rad(90) - (theta - rad(270))));
  186.  
  187.     mask.push([(distance - 60 + maskDistance) * Math.cos(theta) + points[3][0], (distance - 60 + maskDistance) * Math.sin(theta) + points[3][1]]);
  188.  
  189.     // Roof mask
  190.     fillPath(colors.deepblue, mask);
  191.  
  192.     // Outline chimney (1)
  193.     strokePath(colors.black, [
  194.         mask[7], mask[8], mask[13], mask[14], mask[7]
  195.     ]);
  196.  
  197.     // Outline chimney (2)
  198.     strokePath(colors.black, [
  199.         mask[9], mask[10], mask[11], mask[12], mask[9]
  200.     ]);
  201.  
  202.     // Smoke
  203.     fillCircle(320-2, 160-3, 2, colors.lightgray);
  204.     fillCircle(320, 160, 4, colors.lightgray);
  205.     fillCircle(320+4, 160+5, 7, colors.lightgray);
  206.     fillCircle(320+1, 160+13, 9, colors.lightgray);
  207.     fillCircle(320-3, 160+24, 15, colors.lightgray);
  208.  
  209.     fillPath(colors.darkbrown, points);
  210. /* FINISH ROOF TOP */
  211.  
  212. fill(182, 5, 36, 50, colors.darkbrown); // Door
  213. rect(182, 5, 36, 50, colors.brown); // Door Outline
  214.  
  215. // Windows
  216. for (var _o = 0; _o < 2; _o++) {
  217.     var offset = _o - 1 + _o % 2; // Loop will go -1, 1 instead of 0, 1
  218.     var position = 200 - offset * 90 - 23;
  219.  
  220.     fill(position, 20, 46, 50, settings.windows.color); // Fill
  221.  
  222.     fill(position, 23, 6, 47, colors.crimson); // Right curtain
  223.     fill(position + 40, 23, 6, 47, colors.crimson); // Left curtain
  224.  
  225.     fillPath(colors.crimson, [[position, 60], [position + 23, 70], [position, 70]]); // Right curtain top
  226.     fillPath(colors.crimson, [[position + 46, 60], [position + 23, 70], [position + 46, 70]]); // Left curtain top
  227.  
  228.     fill(position - 5, 14, 56, 5, colors.gray); // Ledge under window
  229.  
  230.     for (var x = 0; x < 2; x++) {
  231.         for (var y = 0; y < 2; y++) {
  232.             rect(position + 23*x, 20 + 25*y, 23, 25, colors.black); // Frame
  233.         }
  234.     }
  235. }
  236.  
  237. // Grass
  238. for (var x = rand(5 / settings.grass.density, 10 / settings.grass.density); x < settings.canvas.width; x += rand(5 / settings.grass.density, 10 / settings.grass.density)) {
  239.     grass(x, 2, rand(settings.grass.minHeight, settings.grass.maxHeight), rad(rand(-settings.grass.angle, settings.grass.angle))); // Grass blade
  240. }
  241.  
  242. // Top Window
  243. fillCircle(200, 115, 20, settings.windows.color);
  244. strokeCircle(200, 115, 20, colors.black);
  245. strokePath(colors.black, [[200, 135], [200, 95]]); // Window frame
  246. strokePath(colors.black, [[180, 115], [220, 115]]); // Window frame
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement