ctx画布图像从底部减小宽度

时间:2019-02-19 12:48:40

标签: javascript jquery canvas

我使用以下代码在画布上绘制了图像,其中我将元素图像与折叠的杯子图像组合了

canvas2(getFullElementImage);

function canvas2(getFullElementImage) {

    var canvas = document.getElementById("finalCanvas");
    var ctx = canvas.getContext("2d");
    var productImg = new Image();
    productImg.onload = function () {
        var iw = productImg.width;
        var ih = productImg.height;
        console.log("height");

        canvas.width = iw;
        canvas.height = ih;

        ctx.drawImage(productImg, 30, 0, productImg.width, productImg.height,
            0, 0, iw, ih);
        loadUpperIMage()
    };
    productImg.src = "https://res.cloudinary.com/faizykhan1212/image/upload/v1550583008/folded_cup_1.png"

    function loadUpperIMage() {
        var img = new Image();
        img.src = getFullElementImage;

        img.onload = function () {

            var iw = img.width;
            var ih = img.height;

            // alert(iw)

            var xOffset = 160, //left padding
                yOffset = 110; //top padding

            var a = 190.0; //image width
            var b = 20; //round ness

            var scaleFactor = iw / (4 * a);

            // draw vertical slices
            for (var X = 0; X < iw; X += 1) {
                var y = b / a * Math.sqrt(a * a - (X - a) * (X - a)); // ellipsis equation
                ctx.drawImage(img, X * scaleFactor, 0, iw / 3, ih, X + xOffset, y + yOffset, 1, 574);

            }
        };
    }

};

这是图像的显示方式:

This is how image is showing

我希望这从底部开始减小宽度,以使其适合带有左和右角的杯子。

谢谢

1 个答案:

答案 0 :(得分:0)

也许您可以将webGL用于此类示例。 这段代码很棒。我对此部分进行了校准:

user.username

      var xOffset = 174, //left padding
          yOffset = 110; //top padding

        var a = 180.0; //image width
        var b = 80; //round ness
canvas2();

function canvas2() {

    var canvas = document.getElementById("finalCanvas");
    var ctx = canvas.getContext("2d");
    var productImg = new Image();
    productImg.onload = function () {
        var iw = productImg.width;
        var ih = productImg.height;
        console.log("height");

        canvas.width = iw;
        canvas.height = ih;

        ctx.drawImage(productImg, 30, 0, productImg.width, productImg.height,
            0, 0, iw, ih);
        loadUpperIMage()
    };
    productImg.src = "https://res.cloudinary.com/faizykhan1212/image/upload/v1550583008/folded_cup_1.png"

    function loadUpperIMage() {
        var img = new Image();
        img.src = "https://res.cloudinary.com/faizykhan1212/image/upload/v1550655724/download-3.png";

        img.onload = function () {

            var iw = img.width;
            var ih = img.height;

            // alert(iw)

            var xOffset = 174, //left padding
                yOffset = 110; //top padding

            var a = 180.0; //image width
            var b = 80; //round ness

            var scaleFactor = iw / (4 * a);

            // draw vertical slices
            for (var X = 0; X < iw; X += 1) {
                var y = b / a * Math.sqrt(a * a - (X - a) * (X - a)); // ellipsis equation
                ctx.drawImage(img, X * scaleFactor, 0, iw / 3, ih, X + xOffset, y + yOffset, 1, 574);

            }
        };
    }

};