Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Canvas 3D drawing using both 2D and 3D context

Tags:

Since the webgl/opengl doesn't support text drawing, so it possible to draw 3D object using 3D context and text drawing using 2D context ?

like image 677
Pointer Avatar asked May 28 '10 11:05

Pointer


People also ask

Can we add both 2D and 3D text on the canvas?

As stated, you cannot do this. However you can put one canvas on top of another and draw to them separately.

Is canvas 2D or 3D?

The Canvas API largely focuses on 2D graphics. The WebGL API, which also uses the <canvas> element, draws hardware-accelerated 2D and 3D graphics.

What is getContext 2D in canvas?

The getContext() function is the function that you use to get access to the canvas tags 2D drawing functions. As of April 2014, there are two types of context that are available to you: 2d and webgl . These provide you with the API that you use to draw on the canvas.

How do you get the rendering context of a canvas?

getContext() The HTMLCanvasElement. getContext() method returns a drawing context on the canvas, or null if the context identifier is not supported, or the canvas has already been set to a different context mode.


2 Answers

No, unfortunately not.

The HTML 5 spec says that if you call getContext on a canvas element that is already in a different context mode and the two contexts are not compatible then return null.

Unfortunately "webgl" and "2d" canvases are not compatible and thus you will get null:

var canvas = document.getElementById('my-canvas'); var webgl = canvas.getContext("webgl"); // Get a 3D webgl context, returns a context var twod = canvas.getContext("2d"); // Get a 2D context, returns null 
like image 61
dave Avatar answered Sep 19 '22 04:09

dave


As stated, you cannot do this.

However you can put one canvas on top of another and draw to them separately. I've done this before and it can work out quite well.

like image 35
Drew Noakes Avatar answered Sep 21 '22 04:09

Drew Noakes