# Assign a canvas by ID as the texture

**URL:** <https://forum.babylonjs.com/t/assign-a-canvas-by-id-as-the-texture/3141>\
**Category:** Questions\
**Created:** [May 10, 2019, 10:50pm UTC](https://forum.babylonjs.com/t/assign-a-canvas-by-id-as-the-texture/3141 "2019-05-10T22:50:27Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![JulesClair](https://avatars.discourse-cdn.com/v4/letter/j/71c47a/32.png) [@JulesClair](https://forum.babylonjs.com/u/JulesClair)\
**Post date:** [May 10, 2019, 10:50pm UTC](https://forum.babylonjs.com/t/assign-a-canvas-by-id-as-the-texture/3141/1 "2019-05-10T22:50:27Z")

</div>

How to assign a canvas by ID as the texture?

```
var canvasTexture = document.getElementById("myCanvas");

function changeTexture(mesh){
     var textureGround = new BABYLON.DynamicTexture("dynamic texture", 512, scene);   
     var canvasTexture = textureGround.getContext();

     var mat = new BABYLON.StandardMaterial("mat", scene);
     var texture = new BABYLON.Texture(canvasTexture , scene); // variable does not apply
     mat.diffuseTexture = texture;
     mesh.material = mat;
}
var mesh = scene.getMeshByName("myWonderfulMesh");
changeTexture(mesh)
```

---

<div class="post-metadata">

**Author:** ![nogalo](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/nogalo/32/120_2.png) [@nogalo](https://forum.babylonjs.com/u/nogalo)\
**Post date:** [May 10, 2019, 11:44pm UTC](https://forum.babylonjs.com/t/assign-a-canvas-by-id-as-the-texture/3141/2 "2019-05-10T23:44:07Z")

</div>

Hi,

My first thought is to create screenshot of the canvas and then use that image as a texture. You can check that out here.

[https://doc.babylonjs.com/how\_to/render\_scene\_on\_a\_png](https://doc.babylonjs.com/how_to/render_scene_on_a_png)

So basically, you save canvas content as image (png) and then use that image as a texture.

---

<div class="post-metadata">

**Author:** ![JulesClair](https://avatars.discourse-cdn.com/v4/letter/j/71c47a/32.png) [@JulesClair](https://forum.babylonjs.com/u/JulesClair)\
**Post date:** [May 11, 2019, 12:02am UTC](https://forum.babylonjs.com/t/assign-a-canvas-by-id-as-the-texture/3141/3 "2019-05-11T00:02:34Z")

</div>

Thank you @nogalo . I’ll mark this thread as answered because that answered my question. 🙂

---

<div class="post-metadata">

**Author:** ![JulesClair](https://avatars.discourse-cdn.com/v4/letter/j/71c47a/32.png) [@JulesClair](https://forum.babylonjs.com/u/JulesClair)\
**Post date:** [May 11, 2019, 12:16am UTC](https://forum.babylonjs.com/t/assign-a-canvas-by-id-as-the-texture/3141/4 "2019-05-11T00:16:17Z")

</div>

@nogalo I do not understand how to use the screenshot in the `new BABYLON.Texture()`. DO I have to give the screenshot an ID and then use the id in the `new BABYLON.Texture)`? Can you please help me out?

```
function getsnapShaot(){
  BABYLON.Tools.CreateScreenshot(engine, camera, { width: 1024, height: 300 }, function (data) {
     var img = document.createElement("img");
     img.src = data;
     document.body.appendChild(img);  
  });
}

```

and then how to use it in the `texture` ?

`var texture = new BABYLON.Texture(canvasTexture , scene);`

---

<div class="post-metadata">

**Author:** ![Gijs](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/gijs/32/44_2.png) [@Gijs](https://forum.babylonjs.com/u/Gijs)\
**Post date:** [May 11, 2019, 12:24am UTC](https://forum.babylonjs.com/t/assign-a-canvas-by-id-as-the-texture/3141/5 "2019-05-11T00:24:06Z")

</div>

`var texture = new BABYLON.DynamicTexture("dynamic texture", document.getElementById("myCanvas"), scene);`

---

<div class="post-metadata">

**Author:** ![JulesClair](https://avatars.discourse-cdn.com/v4/letter/j/71c47a/32.png) [@JulesClair](https://forum.babylonjs.com/u/JulesClair)\
**Post date:** [May 11, 2019, 12:37am UTC](https://forum.babylonjs.com/t/assign-a-canvas-by-id-as-the-texture/3141/6 "2019-05-11T00:37:13Z")

</div>

@Gijs Thank you… 🙂
