你能加载一个img标签吗;s src在javascript文件中,然后将其绘制到画布上
Can you load an img tag's src inside javascript file and then draw it to the canvas?
index.html
<html lang="en-US">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=8" />
<title>DCView</title>
<script type="text/javascript" src="dcview.js"></script>
</head>
<body>
<div style="float:center; text-align:center;">
<canvas id="dcviewCanvas" width="1024" height="1024"/>
<img id="bgImage" src="" hidden="true"/>
</div>
</body>
</html>
dcview.js
var backgroundImage,
backgroundImageSrc,
canvas,
context;
window.onload = function()
{
//Initially set the background image source to DCView.png
backgroundImageSrc = "Foundation''WebService''Downloads''DCView.png";
//Load the background image with the given source
loadBackgroundImage(backgroundImageSrc);
}
function loadBackgroundImage(imageSrc)
{
canvas = document.getElementById("dcviewCanvas");
context = canvas.getContext("2d");
backgroundImage = document.getElementById("bgImage");
backgroundImage.src = imageSrc;
context.drawImage(backgroundImage, 0, 0);
}
当我尝试在chrome中加载index.html时,它不会加载背景图像。但是,当我将img标记的src硬编码到该路径时,它会继续加载它。
chrome给我的唯一错误是:
未捕获的语法错误:意外的标记。
它将此错误指向dcview.js.中的第47行
我做这件事的方式完全错了吗?我是HTML和Javascript的初学者。
谢谢,
维拉特
更新
dcview.js
window.onload = function()
{
//Initially set the background image source to DCView.png
backgroundImageSrc = 'Foundation/WebService/Downloads/Layout_Basement.png';
//Load the background image which is initially set to DCView.png
loadBackgroundImage(backgroundImageSrc);
//Load the other various pin images;
loadPinImages();
}
function loadBackgroundImage(imageSrc)
{
context = document.getElementById('dcviewCanvas').getContext('2d');
backgroundImage = new Image();
backgroundImage.src = imageSrc;
backgroundImage.onload = function(){
context.drawImage(backgroundImage, 0, 0);
};
}
更新
一旦我加载了背景图像,我在上面绘制的其他图像就会被绘制在背景图像下面。想法?
window.onload = function()
{
//Initially set the background image source to DCView.png
backgroundImageSrc = 'Foundation/WebService/Downloads/Layout_Basement.png';
//Load the background image which is initially set to DCView.png
loadBackgroundImage(backgroundImageSrc);
canvas = document.getElementById('dcviewCanvas');
context = canvas.getContext('2d');
context.drawImage(sortationLanePinBlue, 0, 0);
}
您可以使用Javascript Image类绘制到画布上。请参阅这篇关于MDN的文章。
相关文章:
- Javascript运行php文件,然后下载文件
- javascript:发送带有音频文件的POST,然后重定向到新页面
- 多个下载链接到一个zip文件,然后再下载javascript
- 文件上传:检查文件格式,然后调用srvlet
- 先在Angular中加载配置文件,然后再加载其他文件
- 加载文件,然后调用回调函数
- 如何获取XML值,然后使用javascript将它们输出到htm文件中
- 如何通过URL下载文件,然后获取其名称
- 更改音频速度,然后另存为新文件
- 通过 AJAX 调用 PHP 文件,将 $_GET 变量传递到 MySQL 查询中,然后回显到响应中
- 将图像输出到浏览器,然后再将其保存到文件夹
- 需要一个文件,然后使用一个函数,而不显式地将所需文件用作命名空间
- 如何将javascript文件插入到iframe中,然后调用插入的javascript中的函数
- 使用load()加载外部文件,然后使用fancybox插件进行修改并显示
- 如何使用 Response 类创建要下载的文件,然后与客户端交互
- JQuery启动文件下载,然后运行一个函数
- php检测浏览器窗口宽度,然后包括适当的php文件
- 方法来查看jQuery文件是否已完全加载,然后执行与其相关的函数
- 使用JavaScript,我可以上传一个word文件并使用.replaces然后另存为新文档吗
- Gulp-Concat文件的顺序然后是任何其他的js文件