F
F
Ferret Common2018-02-25 11:11:30
JavaScript
Ferret Common, 2018-02-25 11:11:30

How to put an image in the background of a rectangle in canvas?

Faced such a problem: you need to create an object moving along the bottom of the canvas, the background of which (object) is a picture. help me please

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Oleg, 2018-02-25
@XOPEK_32

var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');
var img = new Image();
var height = 180;
var width = 180;
img.onload = function() {
  context.save();
    context.rect(0, 0, 200, 200);//Здесь первый 0=X
    context.clip();
    context.drawImage(img,0, 0,width,height);//Первый 0=X
    context.restore();
};
img.src = 'http://vikup-msk.ru/wp-content/uploads/2015/10/1445787630_in_transit.png';

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question