简体   繁体   English

2D 画布(在 3D 画布后面)不绘制图像

[英]2D Canvas (behind a 3D Canvas) not drawing images

I'm currently exploring with Three.js, and working on a small project of mine.我目前正在探索 Three.js,并致力于我的一个小项目。 The project consists on having a canvas focused on 3D models and animations, and another one behind, which handles the simpler 2D work.该项目包括拥有一个专注于 3D 模型和动画的画布,以及另一个处理更简单的 2D 工作的画布。

I've set up the 3D canvas properly, so it's background is transparent, and I can see boxes I draw manually on the 2D canvas, which leads me to assume the setup is right.我已经正确设置了 3D 画布,所以它的背景是透明的,我可以看到我在 2D 画布上手动绘制的框,这让我假设设置是正确的。

The issue I'm having is when it comes to images.我遇到的问题是图像。 I simply cannot get an image to display on the 2D canvas.我根本无法在 2D 画布上显示图像。 I've experimented on a separate project, and could draw Images there, no problem.我已经在一个单独的项目上进行了实验,可以在那里绘制图像,没问题。 The code is pretty basic, and I actually found it here, but is as follows:该代码非常基本,我实际上在这里找到了它,但如下所示:

 window.onload = function() { var canvas = document.getElementById('bgcanvas'); var context = canvas.getContext('2d'); var logoText = new Image(); logoText.onload = function() { context.drawImage(logoText, 69, 50); }; logoText.src = 'images/logotext.png'; }
 #canvas { position: fixed; z-index: 0; } #bgcanvas { z-index: -10; position: fixed; width: 100vw; height: 100vh; }
 <div id="fixedContainer"> <canvas id="bgcanvas"></canvas> <canvas id="canvas"></canvas> </div>

What's going on that I'm unaware of?发生了什么我不知道的事情? Massive thanks in advance!提前致谢!

UPDATE EDIT: The issue was that I had an image on which the top left corner was transparent, and didn't know the image would stretch.更新编辑:问题是我有一个左上角是透明的图像,并且不知道图像会拉伸。 user3412847's comment helped me figure it out user3412847 的评论帮助我弄清楚了

Specifying image width and height is a good habit to get into.指定图像widthheight是一个很好的习惯。 Use this syntax: context.drawImage(image, x, y, width, height) .使用以下语法: context.drawImage(image, x, y, width, height)

Hope this helps.希望这可以帮助。

I'm guessing you don't have an image at that path;我猜你在那个路径上没有图像; It works fine for me with a valid image (eg: http://lorempixel.com/100/100 ):使用有效图像(例如: http : //lorempixel.com/100/100 )对我来说效果很好:

 window.onload = function() { var canvas = document.getElementById('bgcanvas'); var context = canvas.getContext('2d'); var logoText = new Image(); logoText.onload = function() { context.drawImage(logoText, 69, 50); }; logoText.src = 'http://lorempixel.com/100/100'; }
 #canvas { position: fixed; z-index: 0; } #bgcanvas { z-index: -10; position: fixed; width: 100vw; height: 100vh; }
 <div id="fixedContainer"> <canvas id="bgcanvas"></canvas> <canvas id="canvas"></canvas> </div>

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

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