Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I extract a portion of an image in canvas and use it as background image for a div?

This is how my code looks:

document.addEventListener('DOMContentLoaded', function () {
        var canvas = document.querySelector('canvas');
        var ctx = canvas.getContext("2d");
        var imageData = ctx.getImageData(0, 0, 300, 300);
        var tile = {
                        'id': 1,
                        'data': imageData,
                        'dataUrl': imageData.toDataUrl()
                    };
        var div = document.createElement('div');
                div.classList.add('tile');
                grid.appendChild(div);
                div.style.backgroundImage = ('url(' + tile.dataUrl + ')');
});

I'm trying to extract portion of the image on canvas, from (0,0) with height and width 300px, and convert that imageData object into a dataUrl to be used as a background of a div.

I get an error: imageData.toDataUrl() is not a function. How can I achieve this?

Thanks in advance!

like image 261
Ramya Avatar asked Jan 27 '16 09:01

Ramya


People also ask

How do I add a background image in canvas?

To set an image as the full canvas background, first, drag the image to the canvas. Select it and click on the general icon over the image. Choose Set as Full Background. The image will snap to cover the entire spread.

Can canvas have a background image?

There are two ways available in FabricJS, using which we can change the background image of the canvas. First method is by using the Canvas class itself and passing backgroundImage in the second parameter of the class. Second method is to use the setBackgroundColor method. Let's see into each of them with an example.

How do I show part of an image in HTML?

One way to do it is to set the image you want to display as a background in a container (td, div, span etc) and then adjust background-position to get the sprite you want. Just to clarify, you'd set the width and height of the container td, div, span or whatever to 50px to make this work.

How do I show half an image in HTML?

Wrap the image in a div The markup to set up CSS-only cropping is super basic. All you need to do is wrap the img tag in a div . The pug image is 750 pixels wide and 500 pixels high. Let's make it portrait-oriented by maintaining the 500 pixel height, but chopping the width in half to 375 pixels.


1 Answers

toDataURL is an HTMLCanvasElement method you have to call it from the canvas element itself.

You could draw back your resulted imageData to the canvas after you changed its size to the wanted one, but the easiest solution is to use a second, off-screen canvas, where you will draw the first canvas thanks to the context.drawImage method:

// The crop function
var crop = function(canvas, offsetX, offsetY, width, height, callback) {
  // create an in-memory canvas
  var buffer = document.createElement('canvas');
  var b_ctx = buffer.getContext('2d');
  // set its width/height to the required ones
  buffer.width = width;
  buffer.height = height;
  // draw the main canvas on our buffer one
  // drawImage(source, source_X, source_Y, source_Width, source_Height, 
  //  dest_X, dest_Y, dest_Width, dest_Height)
  b_ctx.drawImage(canvas, offsetX, offsetY, width, height,
                  0, 0, buffer.width, buffer.height);
  // now call the callback with the dataURL of our buffer canvas
  callback(buffer.toDataURL());
};


// #main canvas Part

var canvas = document.getElementById('main');
var img = new Image();
img.crossOrigin = "Anonymous";

img.onload = function() {
  canvas.width = this.width;
  canvas.height = this.height;
  canvas.getContext('2d').drawImage(this, 0, 0);
  // set a little timeout before calling our cropping thing
  setTimeout(function() {
    crop(canvas, 100, 70, 70, 70, callback)
  }, 1000);
};

img.src = "https://dl.dropboxusercontent.com/s/1alt1303g9zpemd/UFBxY.png";

// what to do with the dataURL of our cropped image
var callback = function(dataURL) {
  document.body.style.backgroundImage = 'url(' + dataURL + ')';
}
<canvas id="main" width="284" width="383"></canvas>
like image 76
Kaiido Avatar answered Sep 29 '22 07:09

Kaiido