Search code examples
canvasbitmapcreatejseaseljs

How do I convert text to bitmap in createjs?


For instance I have this code:

var text = new createjs.Text("Hello World", "Bold 10px Arial", "#000000");

And I want it to get turned into bitmap.


Solution

  • You can use the cache() method, which creates an off-screen canvas of the content.

    var b = text.getBounds();
    text.cache(b.x, b.y, b.width, b.height, 2);
    

    Here is an example showing cache, as well as exporting the cache to a dataURL, and then making an image with it. http://jsfiddle.net/0wgwaLr6/

    var url = text.cacheCanvas.toDataURL();
    var img = document.createElement("img");
    img.src = url;