简体   繁体   English

从外部文件运行PaperScript / JavaScript不起作用

[英]Running PaperScript/JavaScript from an external file doesn't work

I'm exploring the Paper.js library. 我正在探索Paper.js库。 The first code example in the tutorial works fine. 教程中的第一个代码示例可以正常工作。 But when I move the inline javascript to an external file, as in the second code snippet, it stops displaying anything on the screen. 但是,当我将内联javascript移至外部文件时(如第二个代码片段中所示),它将停止在屏幕上显示任何内容。 In myScript.js, I tried 在myScript.js中,我尝试了

paper.install(window);
window.onload = function() {
 // Create a Paper.js Path to draw a line into it:
    var path = new Path();
    // Give the stroke a color
    path.strokeColor = 'black';
    var start = new Point(100, 100);
    // Move to start and draw a line from there
    path.moveTo(start);
    // Note the plus operator on Point objects.
    // PaperScript does that for us, and much more!
    path.lineTo(start + [ 100, -50 ]);

    }

And using jQuery 并使用jQuery

$(document).ready(function(){
 // Create a Paper.js Path to draw a line into it:
    var path = new Path();
    // Give the stroke a color
    path.strokeColor = 'black';
    var start = new Point(100, 100);
    // Move to start and draw a line from there
    path.moveTo(start);
    // Note the plus operator on Point objects.
    // PaperScript does that for us, and much more!
    path.lineTo(start + [ 100, -50 ]);

    });

with no success. 没有成功。 What did I forget? 我忘记了什么? Thank you for you help 谢谢你的帮助

You either need to refer to a paperscript file or jump through the hoops needed to use javascript directly, as documented at http://paperjs.org/tutorials/getting-started/using-javascript-directly 您可以直接参考paperscript文件,也可以跳过直接使用javascript所需的步骤,如http://paperjs.org/tutorials/getting-started/using-javascript-direct中所述

Here's a fiddle using jquery, paper, coffeescript that may give you a few ideas: http://jsfiddle.net/tATps/ 这是一个使用jquery,paper和coffeescript的小提琴,它可能会给您一些想法: http : //jsfiddle.net/tATps/

$('#foo').text(paper);
canvas = $("#myCanvas")[0];
paper.setup(canvas);
path = new paper.Path.Rectangle(10, 10, 20, 10);
path.strokeColor = "black";
paper.view.draw();

path = new paper.Path.Circle(40, 40, 20);
path.strokeColor = "black";
paper.view.draw();

tool = new paper.Tool()

@view = paper.view
size = view.size
@normalCenter = view.size.divide 2

tool.onMouseMove = (event) =>    
    view.zoom = 2 # yes, I set the zoom every time instead of once.  lazy.
    p = event.point.clone()
    p.x = Math.max p.x, 0
    p.y = Math.max p.y, 0
    movement = @normalCenter.subtract(view.center )
    movement = movement.add p.subtract(@normalCenter).divide(2)
    view.scrollBy(movement)
    $('#foo').text([p, view.center])

If this is related to https://en.wikipedia.org/wiki/Same-origin_policy you need to load the page from a local HTTP server, or disable that check in your browser. 如果与https://en.wikipedia.org/wiki/Same-origin_policy有关,则需要从本地HTTP服务器加载页面,或在浏览器中禁用该检查。 Any of the following are a quick way to setup a local web server. 以下任何一种都是设置本地Web服务器的快速方法。

# Python <3
python -m SimpleHTTPServer

# Python 3
python -m http.server

# NodeJS
npm install http-server -g  # install
http-server                 # run

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM