massoudn
کاربر فعال
Cylindrical Image - External
Scene
Scene
onSelfEvent (load) { import flash.display.BitmapData;
import flash.geom.Matrix;
var cupHeight : Number = bottomBase._y - topBase._y; // The visible height of the cup (about 290 px on this sample)
var topBa***Rad : Number = 0.5 * topBase._width; // The "radii" of the top elliptical base
var topBaseYRad : Number = 0.5 * topBase._height;
var bottomBa***Rad : Number = 0.5 * bottomBase._width; // The "radii" of the bottom elliptical base
var bottomBaseYRad : Number = 0.5 * bottomBase._height;
var radRatio : Number = topBaseYRad / topBa***Rad;
var phi : Number = Math.asin(radRatio); // The angle of "inclination" of the cup towards the observer
var picWidth : Number; // The width of the source image
var realPicHeight : Number; // The height of the source image
var cylPicHeight : Number; // The height of the image projected on the surface of the cup
var numSlices : Number = 16; // The number of slices into which the image will be cut
var sliceWidth : Number; // The width of a slice
var topPicXRadius : Number; // The radius (of the section through the cone) on which the image's top edge will be positioned
var bottomPicXRadius : Number; // The radius on which the image's bottom edge will be positioned
var BasesRadDiff : Number = topBase._width - bottomBase._width; // The difference between the measured radii of the bases of the cup
var topPicY : Number; // Y of the image's top edge
var bottomPicY : Number; // Y of the image's bottom edge
var topPicRadDiff : Number; // The diff... well, it's just an intermediate variable fot calculations
var botPicRadDiff : Number;
var deltaTheta : Number; // The increment of the angle to be used when drawing every new slice on the surface sequentially
var sector : Number; // The angle of the sector engaged by the image over the circumference (in radians)
var theta : Number; // The starting angle where the left edge of the image will be positioned
var curTheta : Number; // The current angle for positioning of every slice along the surface
var leftTheta : Number = Math.PI + 0.1; // The limits of the image's movement - to the left
var rightTheta : Number = Math.PI * 2 - 0.1; // and to the right
var xtopleft : Number; // The coordinates of the slices' vertices used for drawing
var xbottomleft : Number; // by the lineTo(x,y) method
var xtopright : Number;
var xbottomright : Number;
var ytopleft : Number;
var ybottomleft : Number;
var ytopright : Number;
var ybottomright : Number;
var i : Number; // The for() cycle variables
var cylPictureMC : MovieClip = this.createEmptyMovieClip("cylPictureMC", this.getNextHighestDepth());
var containerMC : MovieClip = this.createEmptyMovieClip("containerMC", this.getNextHighestDepth());
var sourceBitmap : BitmapData;
var cylMatrix : Matrix;
var imgLoader: MovieClipLoader = new MovieClipLoader();
var imgLoadListener: Object = new Object();
imgLoader.addListener(imgLoadListener);
imgLoadListener.onLoadInit = function (target_mc : MovieClip) {
target_mc._visible = false;
onSourceSelect(target_mc);
};
/*imgLoadListener.onLoadError = function (target_mc : MovieClip, errorCode : String) {
trace(target_mc + ": " + errorCode);
};*/
imgLoader.loadClip("external_image.jpg", containerMC);
function onSourceSelect(sourceMC : MovieClip) {
picWidth = sourceMC._width;
realPicHeight = sourceMC._height;
cylPicHeight = realPicHeight * Math.cos(phi);
sliceWidth = picWidth / numSlices;
sector = picWidth / topBa***Rad;
theta = 1.5 * Math.PI - 0.5 * sector;
deltaTheta = sector / numSlices;
topPicY = topBase._y + 0.5 * (cupHeight - cylPicHeight);
bottomPicY = topPicY + cylPicHeight;
topPicRadDiff = BasesRadDiff * (bottomBase._y - topPicY) / cupHeight;
botPicRadDiff = BasesRadDiff * (bottomBase._y - bottomPicY) / cupHeight;
topPicXRadius = bottomBa***Rad + topPicRadDiff;
topPicYRadius = topPicXRadius * radRatio;
bottomPicXRadius = bottomBa***Rad + botPicRadDiff;
bottomPicYRadius = bottomPicXRadius * radRatio;
sourceBitmap = new BitmapData(picWidth, realPicHeight, false, 0xFFFFFFFF);
sourceBitmap.draw(sourceMC);
cylPictureMC._x = topBase._x;
cylPictureMC._y = topPicY;
drawCylPicture();
};
function drawCylPicture() {
cylPictureMC.clear();
curTheta = theta;
for (i = 0; i < numSlices; i++) {
if (curTheta < rightTheta) {
xtopleft = topPicXRadius * Math.cos(curTheta);
ytopleft = - topPicYRadius * Math.sin(curTheta);
xbottomleft = bottomPicXRadius * Math.cos(curTheta);
ybottomleft = cylPicHeight - bottomPicYRadius * Math.sin(curTheta);
xtopright = topPicXRadius * Math.cos(curTheta + deltaTheta);
ytopright = - topPicYRadius * Math.sin(curTheta + deltaTheta);
xbottomright = bottomPicXRadius * Math.cos(curTheta + deltaTheta);
ybottomright = cylPicHeight - bottomPicYRadius * Math.sin(curTheta + deltaTheta);
cylMatrix = new Matrix(
(xtopright - xtopleft) / sliceWidth, // x scale
(ytopright - ytopleft) / sliceWidth, // y skew
(xbottomleft - xtopleft) / cylPicHeight, // x skew
(ybottomleft - ytopleft) / realPicHeight, // y scale
xtopleft - i * (xtopright - xtopleft), // tx
ytopleft - i * (ytopright - ytopleft) // ty
);
cylPictureMC.beginBitmapFill(sourceBitmap, cylMatrix, false, false);
cylPictureMC.moveTo(xtopleft, ytopleft);
cylPictureMC.lineTo(xtopright, ytopright);
cylPictureMC.lineTo(xbottomright, ybottomright);
cylPictureMC.lineTo(xbottomleft, ybottomleft);
cylPictureMC.endFill();
curTheta += deltaTheta;
}
}
updateAfterEvent();
};
cylPictureMC.onPress = function() {
var prevX : Number = _root._xmouse;
var newX : Number = _root._ymouse;
onMouseMove = function() {
newX = _root._xmouse;
_root.theta += (newX - prevX)/100;
prevX = newX;
if (_root.theta < _root.leftTheta) {
_root.theta = _root.leftTheta;
}
_root.drawCylPicture();
};
};
cylPictureMC.onRelease = cylPictureMC.onReleaseOutside = function() {
delete onMouseMove;
};
}