gpt4 book ai didi

javascript - EaselJS透视图像变换

转载 作者:太空宇宙 更新时间:2023-11-04 15:18:33 24 4
gpt4 key购买 nike

是否可以在html5 canvas中进行透视图像变换?我现在正在使用 EaselJS,找不到满足我需要的解决方案。

最佳答案

要实现透视变换,您需要一个在 3D 中转换对象的选项,但就 CreateJS/EaselJS 使用 2D 变换矩阵而言,您不能这样做。

但如果您在旅途中使用 Skew 和 Scale,您可以获得相似的外观和感觉。

例如我让卡片在平面上移动,这给了卡片一个透视图:

var aspectRatio = 1.777777777777778;
var canvasHeight, canvasWidth, canvasTop, canvasLeft;
var graphicsLoader;
var stage, frameTick;


var calculateSize = function() {
if ((window.innerWidth / window.innerHeight) >= aspectRatio) {
canvasHeight = window.innerHeight;
canvasWidth = canvasHeight * aspectRatio;
canvasTop = 0;
canvasLeft = (window.innerWidth - canvasWidth) / 2;
} else {
canvasWidth = window.innerWidth;
canvasHeight = canvasWidth / aspectRatio;
canvasLeft = 0;
canvasTop = (window.innerHeight - canvasHeight) / 2;
}

var gameCanvas = document.getElementById('exampleScene');
gameCanvas.style.width = canvasWidth + "px";
gameCanvas.style.height = canvasHeight + "px";
gameCanvas.style.top = canvasTop + "px";
gameCanvas.style.left = canvasLeft + "px";
};


//load graphics
graphicsManifet = [
{id: "flatness", src: "https://i.imgur.com/6E1NKLa.png"},
{id: "card", src: "https://i.imgur.com/CXhpG1X.png"},
];

graphicsLoader = new createjs.LoadQueue(true);
graphicsLoader.loadManifest(graphicsManifet);
graphicsLoader.on("complete", function() {
initScene();
}, this);


function initScene() {

stage = new createjs.Stage("exampleScene");
createjs.Ticker.on("tick", frameTick);

//draw
var flatness = new createjs.Bitmap(graphicsLoader.getResult("flatness"));
flatness.x = 200;

var card = new createjs.Bitmap(graphicsLoader.getResult("card"));
card.alpha = 0;


stage.addChild(
flatness,
card
);

var animationTime = intervalTime = 800;


createjs.Tween.get()
.wait(0)
.call(function() {
movePerspective(card, 280, 10, 0);
card.alpha = 1;
})
.wait(intervalTime)
.call(function() {
movePerspective(card, 1530, 10, animationTime);
card.alpha = 1;
})
.wait(intervalTime)
.call(function() {
movePerspective(card, 1570, 680, animationTime);
card.alpha = 1;
})
.wait(intervalTime)
.call(function() {
movePerspective(card, 230, 680, animationTime);
card.alpha = 1;
})
.wait(intervalTime)
.call(function() {
movePerspective(card, 280, 10, animationTime);
card.alpha = 1;
}).loop = true;



};


function frameTick(e) {
stage.update(e);
}

movePerspective = function(object, x, y, time) {
typeof time != "number" ? time = 1000 : "";
var scales = {
y: y,
scaleX : 0.887073905 + y*0.000142045, // this numbers I calculated depending
scaleY : 0.761221624 + y*0.000284091 // on perspective angle of flatness board
};
var skAspect = 0.00673453853 - y * 0.000000654928977;
var cardSkewX = (6.5991 - skAspect * x);
var pointskew = {
x: x,
skewX: cardSkewX
};

createjs.Tween.get(object).to({
x: pointskew.x,
y: scales.y,
skewX: pointskew.skewX,
scaleX: scales.scaleX,
scaleY: scales.scaleY
}, time, createjs.Ease.getPowInOut(3));
};


window.onload = function() {
window.addEventListener("resize", function() {
calculateSize();
});
};

//set framerate
createjs.Ticker.timingMode = createjs.Ticker.RAF_SYNCHED;
createjs.Ticker.framerate = 60;

$(function() {
calculateSize();
});
#exampleScene {
position: absolute;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://code.createjs.com/createjs-2015.11.26.min.js"></script>
<canvas id="exampleScene" width="1920" height="1080"></canvas>


关于javascript - EaselJS透视图像变换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11864446/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com