gpt4 book ai didi

javascript - paper.js 将变量传递给 javascript

转载 作者:行者123 更新时间:2023-12-03 01:39:25 24 4
gpt4 key购买 nike

我有一个简单的应用程序,可以绘制直线、矩形和多边形。

我正在使用 paper.js,但不确定如何控制流程。

仅当单击线条(按钮)时才应绘制线条。目前,默认情况下,线条是在 Canvas 上绘制的。

<!-- templates/index.html -->
<html>
<head>
<title>Annotation Tool</title>

<!-- CSS Files -->
<link rel="stylesheet" href="/static/node_modules/bootstrap/dist/css/bootstrap.min.css">
<link rel="stylesheet" href="/static/css/style.css">

<!-- Javascript files -->
<script src="/static/node_modules/jquery/dist/jquery.min.js" charset="utf-8"></script>
<script src="/static/node_modules/bootstrap/dist/js/bootstrap.min.js" charset="utf-8"></script>
<script src="/static/js/scripts.js" charset="utf-8"></script>

<!-- Paper files -->
<script src="/static/node_modules/paper/dist/paper-full.min.js" charset="utf-8"></script>
<script type="text/paperscript" src="/static/js/paperscript.js" charset="utf-8" canvas="myCanvas"></script>


</head>
<body>
<script>

</script>
<div class="container">

<div class="row row-bordered">
<div class="btn-toolbar">
<button type="button" class="btn btn-primary" id="draw-line">Line</button>
<button type="button" class="btn btn-primary" id="draw-rect">Rectangle</button>
<button type="button" class="btn btn-primary" id="draw-poly">Polygon</button>
</div>
</div>

<canvas id="myCanvas"></canvas>
<!--<div class="row">-->
<!--<img src="/static/images/lena.png" alt="Italian Trulli">-->
<!--</div>-->

</div>


</body>
</html>

我的 paperscript.js

tool.minDistance = 10;

var path;

function onMouseDown(event) {
// Create a new path and give it a stroke color:
path = new Path();
path.strokeColor = '#00000';

// Add a segment to the path where
// you clicked:
path.add(event.point);
}

function onMouseDrag(event) {
// Every drag event, add a segment
// to the path at the position of the mouse:
path.add(event.point);
}
function internalClicked() {
alert('clicked!');
}

globals.onMouseDown = onMouseDown
globals.onMouseDrag = onMouseDrag
globals.internalClicked = internalClicked

//脚本.js

var globals = {}

$(document).ready(function(){
$('#draw-line').click(function(){
// how can I pass this function to paper script, so that before drawing the line I can check if the draw-line is clicked.
// or how can I override the mouseclickevents of paperjs in javascript.
})
})

最佳答案

关于 Paper.js 和其他 JavaScript 代码之间的交互问题,我认为最好的方法是直接在 JavaScript 代码中使用 Paper.js而不是使用 PaperScript。
主要区别在于您需要设置 Canvas 并且必须通过 paper 对象进行 API 调用。
documentation以获得更详细的解释。

这样做,您将能够更好地将绘图逻辑集成到代码中,并且不必依赖诸如全局变量之类的坏东西。

以下示例说明了如何实现“画线”按钮。

// wait for DOM to be loaded
$(document).ready(function ()
{
// init paper in your canvas
paper.setup($('#myCanvas').get(0));

// on button click
$('#draw-line').click(function ()
{
// draw a line between 2 random points
new paper.Path.Line({
from : paper.Point.random().multiply(paper.view.size),
to : paper.Point.random().multiply(paper.view.size),
strokeColor: 'black'
});
});
});
canvas {
position: fixed;
top:0;
left:0;
width: 100vw;
height: 100vh;
z-index:-1;
}
<html>
<head>
<title>Line Tool</title>

<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/paper.js/0.11.5/paper-core.min.js"></script>

</head>
<body>

<button type="button" id="draw-line">Line</button>

<canvas id="myCanvas"></canvas>

</body>
</html>

关于javascript - paper.js 将变量传递给 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50927917/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com