Search code examples
javascriptlibrariesresponsivepaperjs

Paper.js add 2 more points to corners for more height


I'm starting out with paper.js and have some issues on adding and positioning new points. I want to put 2 new point to the bottom left and bottom right corner for make more height. Right now I have play with the demo code and I have this: (see image below) I want to use it as a background that moves.

1. How I add 2 more points to make more room and height?
2. When I achieve this, how I control it to make it responsive (tablet, mobile etc.)?

Here's the example code working.

Heres the code

  <script type="text/paperscript" canvas="myCanvas">

  var width, height, center;
  var points = 4;
  var smooth = true;
  var path = new Path();
  var mousePos = view.center / 2;
  var pathHeight = mousePos.y;
  var topLeft = view.center - [80, 80];
  var bottomRight = view.center + [80, 80];
  path.fillColor = {
    gradient: {
            stops: ['#532e8e', '#662e8f']
        },
        origin: topLeft,
        destination: bottomRight
  }
  initializePath();

  function initializePath() {
  center = view.center;
  width = view.size.width;
  height = view.size.height / 4;
  path.segments = [];
  path.add(view.bounds.bottomLeft);
  for (var i = 1; i < points; i++) {
    var point = new Point(width / points * i, center.y);
    path.add(point);
  }
  path.add(view.bounds.bottomRight);
//  path.fullySelected = true;
    console.log(path.segments);
  }

  function onFrame(event) {
  pathHeight += (center.y - mousePos.y - pathHeight)/2;
  for (var i = 1; i < points; i++) {
    var sinSeed = event.count + (i + i % 10) * 100 /2;
    var sinHeight = Math.sin(sinSeed / 200) * pathHeight /2;
    var yPos = Math.sin(sinSeed / 100) * sinHeight + height / 1;
    path.segments[i].point.y = yPos;
  }
  if (smooth)
    path.smooth({ type: 'continuous' });
  }

  // Reposition the path whenever the window is resized:
  function onResize(event) {
  initializePath();
  }

  </script>

Solution

  • To make more height, if I understand correctly, you can add two more points at the beginning / end of the path (lines 26 and 32), and iterate through 2 more points (line 40): working example:

      var width, height, center;
      var points = 12;
      var smooth = true;
      var path = new Path();
      var mousePos = view.center / 2;
      var pathHeight = mousePos.y;
      var topLeft = view.center - [80, 80];
      var bottomRight = view.center + [80, 80];
      path.fillColor = {
        gradient: {
                stops: ['#532e8e', '#662e8f']
            },
            origin: topLeft,
            destination: bottomRight
      }
      initializePath();
    
      function initializePath() {
      center = view.center;
      width = view.size.width;
      height = view.size.height / 4;
      path.segments = [];
      path.add(view.bounds.bottomLeft);
      path.add(view.bounds.topLeft);
      for (var i = 1; i < points; i++) {
        var point = new Point(width / points * i, center.y);
        path.add(point);
      }
      path.add(view.bounds.topRight);
      path.add(view.bounds.bottomRight);
      path.fullySelected = true;
    console.log(path.segments);
      }
    
      function onFrame(event) {
      pathHeight += (center.y - mousePos.y - pathHeight)/2;
      for (var i = 1; i < points+2; i++) {
        var sinSeed = event.count + (i + i % 10) * 100 /2;
        var sinHeight = Math.sin(sinSeed / 200) * pathHeight /2;
        var yPos = Math.sin(sinSeed / 100) * sinHeight + height / 1;
        path.segments[i].point.y = yPos;
      }
      if (smooth)
        path.smooth({ type: 'continuous' });
      }
    
      // Reposition the path whenever the window is resized:
      function onResize(event) {
      initializePath();
      }
    

    To make it responsive you can use the onResize event to change the behaviour depending on your canvas size.