[英]How to enable Javascript in Jade template to access DOM?
I want to draw on a HTML5 canvas in a Jade template.我想在 Jade 模板中绘制 HTML5 canvas。 To do this, I need to use some Javascript like this:
为此,我需要像这样使用一些 Javascript:
function tileAllCanvases(){
var c = document.getElementById("canvas");
var ctx = c.getContext("2d");
//draw things
}
My Jade template is:我的玉模板是:
doctype html
html
head
title = title
body
canvas(id="canvas",width="7000",height="4000",style="border:1px solid #d3d3d3;")
script.
!{drawing.tileAllCanvases()}
The document.getElementById("canvas")
call gives me the error document is not defined
. document.getElementById("canvas")
调用给我错误document is not defined
。 I think this is happening because Jade is rendering the HTML server-side and therefore it can't access the DOM.我认为这是因为 Jade 正在呈现 HTML 服务器端,因此它无法访问 DOM。 So I guess I need to have this code called client-side.
所以我想我需要将这段代码称为客户端。 I tried adding the following to my Jade template:
我尝试将以下内容添加到我的 Jade 模板中:
script(src='../main.js', type='text/javascript')
instead of the script.
而不是
script.
call, where main.js
is a file that, among other things, calls the canvas drawing code, but this caused an ERROR 404 NOT FOUND
when I opened the page client-side.调用,其中
main.js
是一个文件,除其他外,它调用 canvas 绘图代码,但这导致在我打开客户端页面时出现ERROR 404 NOT FOUND
。 Maybe because the client-side code is looking for '../main.js'
and of course it can't find it because that file is on the server.可能是因为客户端代码正在寻找
'../main.js'
,当然它找不到它,因为该文件在服务器上。
So...what can I do to draw on a canvas
element using code that's passed to Jade?那么...我该怎么做才能使用传递给 Jade 的代码绘制
canvas
元素? Or, more generally, how do I draw on a canvas
element in a page that I'm generating via a server-side rendering engine?或者,更一般地说,如何在通过服务器端呈现引擎生成的页面中绘制
canvas
元素?
Edit: looks like I can use node-canvas to create a canvas-like object server-side, but I don't quite understand how to then include this data in the page that I'm rendering.编辑:看起来我可以使用node-canvas创建一个类似画布的 object 服务器端,但我不太明白如何将这些数据包含在我正在呈现的页面中。 I see that I can save the canvas object as a PNG and then stream it, but that makes things much more complicated because now I have to handle storage of these dynamically generated files...hopefully there's an easier way to include the
node-canvas
data in my page?我看到我可以将 canvas object 保存为 PNG,然后将其保存为 stream,但这让事情变得更加复杂,因为现在我必须处理这些动态生成的文件的存储......希望有一种更简单的方法来包含
node-canvas
我页面中的数据?
I figured out a solution for this using node-canvas.我想出了一个使用节点画布的解决方案。 Basically it works like this:
基本上它是这样工作的:
const Canvas = require('canvas');
function drawOnCanvas(data,width,height){
let canvas = Canvas.createCanvas(width,height);
drawThings(data,canvas); //code for drawThings() is the same as with the DOM version of Canvas
return canvas.toDataURL();
}
Call this function before rendering the page, so that dataURL
can be exposed to Jade as a parameter:在渲染页面之前调用这个 function,这样
dataURL
就可以作为参数暴露给 Jade:
const express = require('express');
const router = express.Router();
const Drawing = require('../drawing.js');
const drawing = new Drawing();
const dataURL = drawing.drawOnCanvas(someData,100,100);
router.get('/', function(req, res, next) {
res.render('drawing', {
title: 'Drawing',
dataURL: dataURL
});
});
module.exports = router;
And then use dataURL
to create the bitmap in the template.然后使用
dataURL
在模板中创建 bitmap。
doctype html
html
head
title= title
body
img(src=dataURL)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.