avr39ripe

jsCanvasBezierQuadraticCurves

May 12th, 2021
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html lang="en" id="html">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>GalleryClass</title>
  6.     <style>
  7.         html,
  8.         body {
  9.             display: flex;
  10.             align-items: center;
  11.             justify-content: center;
  12.         }
  13.  
  14.         .container {
  15.             display: flex;
  16.             flex-direction: row;
  17.             justify-content: center;
  18.             align-content: center;
  19.             align-items: center;
  20.             margin-top: 30px;
  21.             position: relative;
  22.         }
  23.         canvas {
  24.             margin: 10px;
  25.             border: 1px solid black;
  26.             background-image: url('grid.png');
  27.         }
  28.     </style>
  29. </head>
  30. <body>
  31.     <div id="container" class="container">
  32.         <canvas id="canvas" width="650" height="450">Please update your browser!</canvas>
  33.     </div>
  34.     <script>
  35.         'use strict'
  36.  
  37.         function drawQuadraticCurve(ctx, bX, bY, eX, eY, cpX, cpY) {
  38.             ctx.beginPath();
  39.             ctx.strokeStyle = 'blue';
  40.             ctx.moveTo(bX, bY);
  41.             ctx.lineTo(cpX, cpY);
  42.             ctx.lineTo(eX, eY);
  43.             ctx.stroke();
  44.  
  45.             ctx.beginPath();
  46.             ctx.strokeStyle = 'red';
  47.             ctx.lineWidth = 2;
  48.             ctx.moveTo(bX, bY);
  49.             ctx.quadraticCurveTo(cpX, cpY, eX, eY);
  50.             ctx.stroke();
  51.         }
  52.  
  53.         function drawBezierCurve(ctx, bX, bY, eX, eY, cpAX, cpAY, cpBX, cpBY) {
  54.             ctx.beginPath();
  55.             ctx.strokeStyle = 'blue';
  56.             ctx.moveTo(bX, bY);
  57.             ctx.lineTo(cpAX, cpAY);
  58.             ctx.moveTo(cpBX, cpBY)
  59.             ctx.lineTo(eX, eY);
  60.             ctx.stroke();
  61.  
  62.             ctx.beginPath();
  63.             ctx.strokeStyle = 'red';
  64.             ctx.lineWidth = 2;
  65.             ctx.moveTo(bX, bY);
  66.             ctx.bezierCurveTo(cpAX, cpAY, cpBX, cpBY, eX, eY);
  67.             ctx.stroke();
  68.         }
  69.  
  70.  
  71.         {
  72.             const canvas = document.getElementById('canvas');
  73.             canvas.width = 650;
  74.             canvas.height = 450;
  75.             const ctx = canvas.getContext('2d');
  76.  
  77.             let cpAX = 200;
  78.             let cpAY = 50;
  79.  
  80.             let cpBX = 350;
  81.             let cpBY = 200;
  82.  
  83.             let bX = 100;
  84.             let bY = 100;
  85.  
  86.             let eX = 300;
  87.             let eY = 300;
  88.  
  89.             drawQuadraticCurve(ctx, 100, 100, 300, 300, 350, 50);
  90.             drawBezierCurve(ctx, 100, 100, 300, 300, 200, 50, 350, 200);
  91.  
  92.         }
  93.     </script>
  94. </body>
  95. </html>
Advertisement
Add Comment
Please, Sign In to add comment