通过从文本区域获取代码,在画布中运行处理代码
Running processing code in canvas by getting the code from textarea
我正试图从textbox中获取处理代码,并使其在画布上运行,但我不知道发生了什么,有人能帮我吗?当我调试时,上面写着ctx.compile();不是函数,我怎样才能使它正常工作?这是我使用的代码。
<!DOCTYPE Html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="styles/style2.css" >
<script src="processing-1.4.1.js"></script>
<script type="text/javascript">
function submitTryit() {
var text = document.getElementById("textareaCode").value;
var ifr = document.createElement("iframe");
ifr.setAttribute("frameborder", "0");
ifr.setAttribute("id", "iframeResult");
document.getElementById("iframewrapper").innerHTML = "";
document.getElementById("iframewrapper").appendChild(ifr);
var ifrw = (ifr.contentWindow) ? ifr.contentWindow : (ifr.contentDocument.document) ? ifr.contentDocument.document : ifr.contentDocument;
ifrw.document.open();
ifrw.document.write(text);
ifrw.document.close();
canvas();
if (ifrw.document.body && !ifrw.document.body.isContentEditable) {
ifrw.document.body.contentEditable = true;
ifrw.document.body.contentEditable = false;
}
function canvas() {
var ifrr = document.getElementById('iframeResult');
var iframediv = (ifrr.contentWindow.document) ? ifrr.contentWindow.document : (ifrr.contentDocument.document) ? ifrr.contentDocument.document : ifrr.contentDocument;
var canv = document.createElement('CANVAS');
canv.setAttribute('id', 'mycanvas');
var ctx = canv.getContext("2d");
ctx.compile();
iframediv.body.appendChild(canv);
}
}
function compile(){
var processingCode = document.getElementById('textCode').value;
var jsCode = Processing.compile(processingCode).sourceCode;
}
</script>
</head>
<body>
<div class="container">
<!--Iframe-->
<div class="iframecontainer">
<div id="iframewrapper" class="iframewrapper">
</div>
</div>
<!--PJS text field-->
<div class="PJStextwraper">
<div class="overPJStext">
</div>
<textarea id="textCode" spellcheck="false" autocomplete="off" wrap="logical"> int x,y,w;
float vx,vy;
void setup() {
size(300,200);
x = int(random(width));
y = int(random(height));
vx = random(5);
vy = random(5);
w = int(10+random(10));
}
void draw() {
background(140,70,60);
ellipse(x,y,w,w);
x += vx;
y += vy;
//
if(x > width || x < 0) {
vx *= -1;
}
if(y > height || y < 0) {
vy *= -1;
}
}
</textarea>
</div>
<button class="BT" type="button" onclick="submitTryit()">Run »</button>
</div> <!-- End container-->
</body>
</html>
由于您试图调用2d画布上下文中不存在的函数,因此会出现错误"ctx.compile();不是函数"。如果我理解正确的话,您正在尝试运行自己的compile
函数,并将其呈现在刚刚创建的画布上。
要做到这一点,你需要改变一些事情,尝试以下操作,看看它是否有效:
function canvas() {
var ifrr = document.getElementById('iframeResult');
var iframediv = (ifrr.contentWindow.document) ? ifrr.contentWindow.document : (ifrr.contentDocument.document) ? ifrr.contentDocument.document : ifrr.contentDocument;
var canv = document.createElement('canvas');
canv.setAttribute('id', 'mycanvas');
// The ctx will be created in an instance of Processing.
// So no need for the following line.
// var ctx = canv.getContext("2d");
compile(canv);
iframediv.body.appendChild(canv);
}
// Here you can pass the canvas where you want to render the code.
function compile(canv) {
var processingCode = document.getElementById('textCode').value;
var jsCode = Processing.compile(processingCode).sourceCode;
// Now that you have the code in JS, you can create an instance of Processing.
// But, what you get from the compiler is a string,
// so you need to convert the string to JS.
// For instance, I use here eval(jsCode)
var processingInstance = new Processing(canv, eval(jsCode));
}
相关文章:
- Angularjs代码未在匿名函数中运行
- 在终端中运行 JavaScript 时(使用 rhino),如何使用 print() 函数在一行中打印
- 我的HTML按钮没有在Javascript中运行
- 从Chrome扩展名中的popup.html文件在background.js文件中运行一个函数
- 在JSP中运行pdftk
- jQuery自动完成在Firefox中运行良好,但在Chrome中则不然
- 在Debian中运行包含load()和print()函数的JS脚本
- javascript没有在aptana3工作室中运行
- 如何让BiwaScheme在JSFiddle中运行
- 如何在angularJS中运行for循环而不使用html标记
- 在其他javascript框架模板中运行angular指令
- 在Android WebView中运行Javascript-onPageFinished循环
- 在父网页的iframe中运行JavaScript
- 谷歌地图:函数返回未定义的值在console.log中运行良好
- 获取docker容器中运行的节点应用程序的IP地址
- 使用javascript在HTML5中运行的时间
- 多人游戏完全在浏览器中运行,服务器仅用于数据库
- 内容脚本是在同一个孤立的世界中运行,还是在不同的孤立世界中运行
- 如何在javascript中的if或else块中运行一条jsp语句
- Meteor发布/订阅无法从服务器文件夹中运行