2017-07-14 60 views
0

我開始使用paper.js,並在添加和定位新點時遇到一些問題。我想在左下角和右下角添加2個新點,以獲得更多高度。現在我玩了演示代碼,我有這個:(見下圖)我想用它作爲移動的背景。

1.我如何增加2點以增加空間和高度?
2.當我達到此目的時,我如何控制它使其響應(平板電腦,手機等)?

這是example code workingPaper.js爲更高的高度添加2個角點

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

爲了讓更多的高度,如果我理解正確的話,你可以在路徑(線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事件改變取決於你的畫布大小的行爲。

+0

好吧,我做了soem改變,我越來越接近。現在,我如何使矢量不觸及畫布的邊緣?繼承人的代碼:https://codepen.io/G-ROS/pen/yXwzBE –