My goal is to wrap a image over a coffee mug virtually using HTML5 and javascript.
I have created a prototype: http://jsfiddle.net/sandeepkum88/uamov2m7/
Currently I am cutting the image in strips of width 1px and then placing those strips on the bezier curve.
var img = new Image();
img.onload = start;
img.src = "http://in-sandeep.printvenue.org/test-images/mug-strap.png";
var pointer = 0;
function start() {
var iw = 198.97826;
var ih = img.height;
var x1 = 50;
var y1 = 40;
var x2 = 97;
var y2 = 60;
var x3 = 152;
var y3 = 40;
// calc unit value for t to calculate bezier curve
var unitT = 1 / iw;
// put image slices on the curve
for (var X = 0, t = 0; X < iw; X++, t+=unitT) {
var xTop = (1-t) * (1-t) * x1 + 2 * (1 - t) * t * x2 + t * t * x3;
var yTop = (1-t) * (1-t) * y1 + 2 * (1 - t) * t * y2 + t * t * y3;
ctx.drawImage(img, X + pointer, 0, 1, ih, xTop, yTop, 0.85, ih-188);
}
}
But I am not satisfied with the result.
Am I doing it wrong somewhere? Is there any better alternative. What if top curve and bottom curve are different.
Is there a way to achieve this using transformation matrix?