Paper.js为角落增加2个点以获得更高的高度

时间:2017-07-14 15:44:14

标签: javascript libraries responsive paperjs

我从paper.js开始,并且在添加和定位新点方面存在一些问题。我想在左下角和右下角加2个新点,以增加高度。现在我玩了演示代码,我有这个:(见下图)我想用它作为移动的背景。

1。我如何增加2点以增加空间和高度?
2.当我实现这一目标时,我如何控制它以使其响应(平板电脑,移动设备等)?

这是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>

1 个答案:

答案 0 :(得分:1)

要获得更高的高度,如果我理解正确,你可以在路径的开头/结尾添加两个点(第26和32行),并再迭代2个点(第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();
  }

要使其响应,您可以使用onResize事件根据画布大小更改行为。