Processing.js和P5.js的功能简介和区别

techbrood 发表于 2016-05-18 13:05:24

标签: processing, p5, 教程, 实例

- +
什么是Processing

Processing是关于数字艺术的编程语言,支持跨平台,语言本身是一个类Java语言,程序文件的后缀为.pde。

什么是Processing.js

为了能让Processing的代码能在Web上工作,John Resig开发了Processing.js,

该JS开发库用来完成两个方面的任务:

  1. 能把Processing语言动态转换成JS,从而在Web环境中执行;

  2. 提供了一套完整的2D图形处理接口(API),直接以JS语言来编程。

显然其最实用的地方是能直接利用现有的大量Processing代码。

什么是P5.js

P5是Processing语言的一个JS移植版本,使其能在Web中工作。它完全使用JavaScript来实现Processing语言相同的功能,但并不会动态翻译Processing语言代码,这一点和Processing.js不同。也就是P5.js差不多等同于Processing.js的JS API部分。但P5.js的功能更单一,角色更专注,且也是Processing基金会唯一支持的项目。


所以,对我而言,如果你想利用已有的Processing(java)代码,你可以使用Processing.js。如果你想直接使用JS创建一些艺术作品(如基本的几何图形、图像处理、交互式动画和操作DOM等),那么推荐使用P5.js。

下面给出两个在线实例,以方便你更直观的了解其使用方式:

例1:使用Processing.js加载和绘制图像

PImage a;  // Declare variable "a" of type PImage
void setup() {
    noLoop(); // rather than drawing as an animation, only call draw() once
    size(200, 200);
    // Load the images for use in the sketch 
    a = loadImage("/uploads/160501/moon.png"); 
} 

void draw() {
image(a, 0, 0); // Display the image at point (0,0) with natural width and height 
    // Display the image again, this time starting at (width/2, 0), 
    // and scaled to 50% in both x and y direction
    image(a, width/2, 0, a.width/2, a.height/2);  
}

例2:使用P5.js加载和绘制图像

var img; // Declare variable 'img'.

function setup() {
    createCanvas(680, 480);
    img = loadImage("/uploads/160501/moon.png"); // Load the image
}

function draw() {
    // Displays the image at its actual size at point (0,0)
    image(img, 0, 0);
    // Displays the image at point (0, height/2) at half size
    image(img, 0, height / 2, img.width / 2, img.height / 2);
}

2016.05.18                      

possitive(19) views25269 comments1

发送私信

最新评论

寻觅良药ˇ 2017-01-17 16:44:07

一般的论坛能不能用
我的论坛站
www.mccake.net


请先 登录 再评论.
相关文章