Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.   <meta charset="UTF-8">
  5.   <title>Paint</title>
  6. </head>
  7. <body>
  8. <label for="selectShapeType">Object type:</label>
  9. <select onchange="dropDown()" id="selectShapeType"></select> <!--onchange-->
  10. <label for="selectShape">Shape drawn:</label>
  11. <select id="selectShape"></select>
  12. <button id="cmbDelete">Slett</button>
  13. <hr>
  14. <canvas id="cvs" style="background-color: antiquewhite"></canvas>
  15. <script>
  16.   //-------------------------------------------------------------------------------------
  17.   //------ Global Variables -------------------------------------------------------------
  18.   //-------------------------------------------------------------------------------------
  19.  
  20.   // DOM model of canvas
  21.   var cvs = null;
  22.   // 2D konrext of canvas
  23.   var ctx = null;
  24.   // DOM model of selected shape
  25.   var selectShapeType = null;
  26.   // DOM model of shapes drawn
  27.   var selectShape = null;
  28.   // DOM model of comando button delete
  29.   var cmbDelete = null;
  30.  
  31.  
  32.    var radius = 5;
  33.  
  34.    var dragging = false;
  35.    var dragStartLocation;
  36.    var snapshot;
  37.  
  38.  
  39.    var selectedShape;
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  //-------------------------------------------------------------------------------------
  48.   //------ Classes and Objects-----------------------------------------------------------
  49.   //-------------------------------------------------------------------------------------
  50.  
  51.  
  52.  
  53.   var EShapeType = {
  54.     Line:       {text: "Linje",     ClosePath: false, Fill: false},
  55.     Pencil:     {text: "Blyant",    ClosePath: false, Fill: false},
  56.     Rectangle:  {text: "Rektangle", ClosePath: true,  Fill: true},
  57.     Circle:     {text: "Sirkel",    ClosePath: true,  Fill: true},
  58.     Oval:       {text: "Oval",      ClosePath: true,  Fill: true},
  59.     Polygon:    {text: "Polygon",   ClosePath: true,  Fill: true}
  60.   };
  61.  
  62.  
  63.   //Different colors to shapes
  64.  
  65.  var colors = ["#e6194b", "#3cb44b", "#ffe119", "#0082c8",
  66.      "#f58231", "#911eb4", "#46f0f0", "#f032e6",
  67.     "#d2f53c", "#fabebe", "#008080", "#e6beff",
  68.     "#aa6e28", "#fffac8", "#800000", "#aaffc3"];
  69.  
  70.  
  71.  
  72.  
  73.   //-------------------------------------------------------------------------------------
  74.   //------ Functions --------------------------------------------------------------------
  75.   //-------------------------------------------------------------------------------------
  76.  
  77.  
  78.  
  79.   // Updates Canvas on interval timeout
  80.   function drawCanvas() {
  81.     ctx.clearRect(0, 0, cvs.width, cvs.height);
  82.  
  83.  
  84.   }
  85.  
  86.   // Fills all shape types user can draw
  87.   function fillSelectShapeTypes() {
  88.     var keys = Object.keys(EShapeType);
  89.     for (var key in keys) {
  90.       var shapeType = keys[key]; // This is actual the shape type!
  91.       var newOption = new Option(EShapeType[shapeType].text, shapeType, false, false); // New option for shape typw
  92.       selectShapeType.options.add(newOption);// Add the new option to select
  93.  
  94.         console.log(shapeType);
  95.     }
  96.  
  97.   }
  98.  
  99.   // Get excact possition for mouse coordinates in canvas
  100.   function getMousePos(aEvent) {
  101.     var rect = cvs.getBoundingClientRect();
  102.     return {
  103.       x: aEvent.clientX - rect.left,
  104.       y: aEvent.clientY - rect.top
  105.     };
  106.   }
  107.  
  108.   //Dropdown list
  109.  
  110.    function dropDown() {
  111.        selectedShape = document.getElementById("selectShapeType").value;
  112.        console.log(selectedShape);
  113.  
  114.    }
  115.  
  116.  
  117.   //Drawing shapes and gives random colors
  118.   function shapes(aEvent){
  119.  
  120.      ctx.fillStyle = colors[Math.floor(Math.random()*16)];  //random colors
  121.      ctx.strokeStyle = "Black"; //black stroke
  122.  
  123.       if(selectedShape === "Rectangle") {
  124.           drawRect(aEvent);
  125.       } else if (selectedShape === "Line"){
  126.           drawLine(aEvent);
  127.       } else if (selectedShape === "Circle"){
  128.           drawCirc(aEvent);
  129.       } else if (selectedShape === "Oval"){
  130.           drawOval(aEvent);
  131.       } else if (selectedShape === "Pencil"){
  132.           drawPencil(aEvent);
  133.       }
  134.  
  135.  
  136.   }
  137.   console.log(selectShapeType);
  138.  
  139.  
  140.   //Pencil
  141.   function drawPencil(aEvent) {
  142.  
  143.  
  144.  
  145.       ctx.lineWidth = radius * 2;
  146.       ctx.lineTo(aEvent.offsetX, aEvent.offsetY);
  147.       ctx.stroke();
  148.       ctx.beginPath();
  149.       ctx.arc(aEvent.offsetX, aEvent.offsetY, radius, 0, Math.PI * 2);
  150.       ctx.fill();
  151.       ctx.beginPath();
  152.       ctx.moveTo(aEvent.offsetX, aEvent.offsetY);
  153.      
  154.  
  155.  
  156.  
  157.   }
  158.  
  159.  
  160.  
  161.   //rectangle shape
  162.  
  163.    function drawRect(position){
  164.  
  165.  
  166.          //ctx.strokeRect(aEvent.x, aEvent.y, 150, 100);
  167.          //ctx.fillRect(aEvent.x, aEvent.y, 150, 100);
  168.  
  169.         ctx.beginPath();
  170.         ctx.strokeRect(dragStartLocation.x, dragStartLocation.y, -(dragStartLocation.x - position.x), -(dragStartLocation.y - position.y));
  171.         ctx.fillRect(dragStartLocation.x, dragStartLocation.y, -(dragStartLocation.x - position.x), -(dragStartLocation.y - position.y));
  172.  
  173.  
  174.      }
  175.  
  176.      
  177.  
  178.   //Line shape
  179.  
  180.   function drawLine(position){
  181.       ctx.beginPath();
  182.       ctx.moveTo(dragStartLocation.x, dragStartLocation.y);
  183.       ctx.lineTo(position.x, position.y);
  184.       ctx.stroke();
  185.   }
  186.  
  187.  
  188.   //circle shape
  189.  
  190.   function drawCirc(position){
  191.       var radiusRound = Math.sqrt(Math.pow(dragStartLocation.x - position.x), 2)
  192.           + Math.pow ((dragStartLocation.y - position.y), 2);
  193.       ctx.beginPath();
  194.       ctx.arc(position.x, position.y, radiusRound, 0, 2*Math.PI, true);
  195.       ctx.fill();
  196.       ctx.stroke();
  197.   }
  198.  
  199.   //Oval shape
  200.  
  201.   function drawOval() {
  202.  
  203.       ctx.beginPath();
  204.       ctx.scale(1, 0.50);
  205.       ctx.beginPath();
  206.       ctx.arc(200, 200, 180, 0, 2 * Math.PI, false);
  207.       ctx.stroke();
  208.  
  209.   }
  210.  
  211.  
  212.  
  213.   function takeScreenshots(){
  214.       snapshot = ctx.getImageData(0, 0, cvs.width, cvs.height);
  215.  
  216.   }
  217.  
  218.   function renewScreenshots(){
  219.       ctx.putImageData(snapshot, 0, 0);
  220.   }
  221.  
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228.   //-------------------------------------------------------------------------------------
  229.   //------ Events -----------------------------------------------------------------------
  230.   //-------------------------------------------------------------------------------------
  231.  
  232.     var mouseIsDown = false;
  233.  
  234.  
  235.   // Event for mouse down
  236.   function mouseDown(aEvent) {
  237.     var pos = getMousePos(aEvent);
  238.  
  239.  
  240.  
  241.     dragging = true;
  242.     dragStartLocation = pos;
  243.     takeScreenshots();
  244.  
  245.     shapes(aEvent);
  246.  
  247.  
  248.   }
  249.  
  250.   // Event for mouse move
  251.   function mouseMove(aEvent) {
  252.     var pos = getMousePos(aEvent);
  253.  
  254.     var position;
  255.  
  256.    if(dragging === true && selectedShape === "Rectangle"){
  257.        renewScreenshots();
  258.        position = pos;
  259.        drawRect(position);
  260.    }
  261.  
  262.    if(dragging === true && selectedShape === "Circle") {
  263.        renewScreenshots();
  264.        position = pos;
  265.        drawCirc(position);
  266.    }
  267.  
  268.    if(dragging === true && selectedShape === "Line"){
  269.        renewScreenshots();
  270.        position = pos;
  271.        drawLine(position);
  272.    }
  273.  
  274.    if(dragging === true && selectedShape === "Oval"){
  275.        renewScreenshots();
  276.        drawOval();
  277.    }
  278.  
  279.    if(selectedShape === "Pencil") {
  280.      drawPencil(aEvent);
  281.    }
  282.  
  283.                                    
  284.    
  285.    }
  286.  
  287.   // Event for mouse up
  288.   function mouseUp(aEvent) {
  289.     var pos = getMousePos(aEvent);
  290.  
  291.  
  292.  
  293.     dragging = false;
  294.  
  295.    
  296.   }
  297.  
  298.   function selectShapeChange(){
  299.     drawCanvas();
  300.   }
  301.  
  302.   function cmbDeleteClick(){
  303.       drawCanvas();
  304.   }
  305.  
  306.   //-------------------------------------------------------------------------------------
  307.  
  308.   //-------------------------------------------------------------------------------------
  309.   //------ Main code --------------------------------------------------------------------
  310.   //-------------------------------------------------------------------------------------
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.   //This function runs after all HTML elements are crated and starts the game
  318.   window.onload = function () {
  319.     cvs = document.getElementById("cvs");
  320.     cvs.width = 1024;
  321.     cvs.height = 512;
  322.     cvs.addEventListener("mousedown", mouseDown, false);
  323.     cvs.addEventListener("mousemove", mouseMove, false);
  324.     cvs.addEventListener("mouseup", mouseUp, false);
  325.     ctx = cvs.getContext('2d');
  326.  
  327.     selectShapeType = document.getElementById("selectShapeType");
  328.     selectShape = document.getElementById("selectShape");
  329.     selectShape.addEventListener("change", selectShapeChange, false);
  330.  
  331.     cmbDelete = document.getElementById("cmbDelete");
  332.     cmbDelete.addEventListener("click",cmbDeleteClick, false);
  333.     fillSelectShapeTypes();
  334.     drawCanvas();
  335.    
  336.     console.log(selectShapeType);
  337.   };
  338.  
  339.   //-------------------------------------------------------------------------------------
  340. </script>
  341. </body>
  342. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement