Zoom Objek on center stage as3 - zooming

buttonZoomIn.addEventListener(MouseEvent.MOUSE_DOWN, zi_begin);
function zi_begin(e:MouseEvent):void
{
if(map.scaleX < 6)
{
map.scaleX += 0.5;
map.scaleY += 0.5;
}
I have images that can be enlarged ... but the image is enlarged from
the center of the image, instead of the center of the stage....
how to keep the image can be enlarged from the center stage not from
center image ?
}

This is my simple code..
zoomin.addEventListener(MouseEvent.CLICK, f_zoomin);
function f_zoomin(e:MouseEvent)
{
zoom = 1;
f_zoom();
}
zoomout.addEventListener(MouseEvent.CLICK, f_zoomout);
function f_zoomout(e:MouseEvent)
{
zoom = -1;
f_zoom();
}
function f_zoom()
{
var coord:Point = map.globalToLocal(new Point(stage.stageWidth/2, stage.stageHeight/2));
map.scaleX += zoom;
map.scaleY += zoom;
map.x = ((2 * stage.stageWidth/2) - (2 * (coord.x * map.scaleX))) / 2;
map.y = ((2 * stage.stageHeight/2) - (2 * (coord.y * map.scaleY))) / 2;
}
Btw your pic like my girlfriend.. LOL :D

Related

How do I allow pointer-events on an HTML5 canvas background while allowing links and buttons to receive pointer-events, too?

I set an HTML5 canvas to a background that looks like interlinked nodes (link). The canvas element works as expected but when I set it to be the background of the page, I couldn't click on links or buttons on the page.
When I set the canvas background to the back of all elements by either declaring a z-index value or editing the HTML so that it's the first element, I can normally click on links, but every time a pointer-event is active on a button (such as a hover) it stops the canvas background from getting the mouse pointer's location.
I expect it's got something to do with the pointer-events, but I can't move past this because I'm unsure as to what to do.
Supposing that you put the content of your document in a div #wrap with a position absolute over the canvas, in order to interact with the particles on canvas you may use the mouse position over the #wrap. In your example I'm using wrap.addEventListener('mousemove', mousemoveHandler, false); instead of canvas.addEventListener('mousemove', mousemoveHandler, false);
(function() {
var canvas, ctx, circ, nodes, mouse, SENSITIVITY, SIBLINGS_LIMIT, DENSITY, NODES_QTY, ANCHOR_LENGTH, MOUSE_RADIUS;
// how close next node must be to activate connection (in px)
// shorter distance == better connection (line width)
SENSITIVITY = 100;
// note that siblings limit is not 'accurate' as the node can actually have more connections than this value that's because the node accepts sibling nodes with no regard to their current connections this is acceptable because potential fix would not result in significant visual difference
// more siblings == bigger node
SIBLINGS_LIMIT = 10;
// default node margin
DENSITY = 50;
// total number of nodes used (incremented after creation)
NODES_QTY = 0;
// avoid nodes spreading
ANCHOR_LENGTH = 20;
// highlight radius
MOUSE_RADIUS = 200;
circ = 2 * Math.PI;
nodes = [];
canvas = document.querySelector('canvas');
resizeWindow();
mouse = {
x: canvas.width / 2,
y: canvas.height / 2
};
ctx = canvas.getContext('2d');
if (!ctx) {
alert("Ooops! Your browser does not support canvas :'(");
}
function Node(x, y) {
this.anchorX = x;
this.anchorY = y;
this.x = Math.random() * (x - (x - ANCHOR_LENGTH)) + (x - ANCHOR_LENGTH);
this.y = Math.random() * (y - (y - ANCHOR_LENGTH)) + (y - ANCHOR_LENGTH);
this.vx = Math.random() * 2 - 1;
this.vy = Math.random() * 2 - 1;
this.energy = Math.random() * 100;
this.radius = Math.random();
this.siblings = [];
this.brightness = 0;
}
Node.prototype.drawNode = function() {
var color = "rgba(255, 0, 0, " + this.brightness + ")";
ctx.beginPath();
ctx.arc(this.x, this.y, 2 * this.radius + 2 * this.siblings.length / SIBLINGS_LIMIT, 0, circ);
ctx.fillStyle = color;
ctx.fill();
};
Node.prototype.drawConnections = function() {
for (var i = 0; i < this.siblings.length; i++) {
var color = "rgba(255, 0, 0, " + this.brightness + ")";
ctx.beginPath();
ctx.moveTo(this.x, this.y);
ctx.lineTo(this.siblings[i].x, this.siblings[i].y);
ctx.lineWidth = 1 - calcDistance(this, this.siblings[i]) / SENSITIVITY;
ctx.strokeStyle = color;
ctx.stroke();
}
};
Node.prototype.moveNode = function() {
this.energy -= 2;
if (this.energy < 1) {
this.energy = Math.random() * 100;
if (this.x - this.anchorX < -ANCHOR_LENGTH) {
this.vx = Math.random() * 2;
} else if (this.x - this.anchorX > ANCHOR_LENGTH) {
this.vx = Math.random() * -2;
} else {
this.vx = Math.random() * 4 - 2;
}
if (this.y - this.anchorY < -ANCHOR_LENGTH) {
this.vy = Math.random() * 2;
} else if (this.y - this.anchorY > ANCHOR_LENGTH) {
this.vy = Math.random() * -2;
} else {
this.vy = Math.random() * 4 - 2;
}
}
this.x += this.vx * this.energy / 100;
this.y += this.vy * this.energy / 100;
};
function initNodes() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
nodes = [];
for (var i = DENSITY; i < canvas.width; i += DENSITY) {
for (var j = DENSITY; j < canvas.height; j += DENSITY) {
nodes.push(new Node(i, j));
NODES_QTY++;
}
}
}
function calcDistance(node1, node2) {
return Math.sqrt(Math.pow(node1.x - node2.x, 2) + (Math.pow(node1.y - node2.y, 2)));
}
function findSiblings() {
var node1, node2, distance;
for (var i = 0; i < NODES_QTY; i++) {
node1 = nodes[i];
node1.siblings = [];
for (var j = 0; j < NODES_QTY; j++) {
node2 = nodes[j];
if (node1 !== node2) {
distance = calcDistance(node1, node2);
if (distance < SENSITIVITY) {
if (node1.siblings.length < SIBLINGS_LIMIT) {
node1.siblings.push(node2);
} else {
var node_sibling_distance = 0;
var max_distance = 0;
var s;
for (var k = 0; k < SIBLINGS_LIMIT; k++) {
node_sibling_distance = calcDistance(node1, node1.siblings[k]);
if (node_sibling_distance > max_distance) {
max_distance = node_sibling_distance;
s = k;
}
}
if (distance < max_distance) {
node1.siblings.splice(s, 1);
node1.siblings.push(node2);
}
}
}
}
}
}
}
function redrawScene() {
resizeWindow();
ctx.clearRect(0, 0, canvas.width, canvas.height);
findSiblings();
var i, node, distance;
for (i = 0; i < NODES_QTY; i++) {
node = nodes[i];
distance = calcDistance({
x: mouse.x,
y: mouse.y
}, node);
if (distance < MOUSE_RADIUS) {
node.brightness = 1 - distance / MOUSE_RADIUS;
} else {
node.brightness = 0;
}
}
for (i = 0; i < NODES_QTY; i++) {
node = nodes[i];
if (node.brightness) {
node.drawNode();
node.drawConnections();
}
node.moveNode();
}
requestAnimationFrame(redrawScene);
}
function initHandlers() {
document.addEventListener('resize', resizeWindow, false);
///////////////////////////////////
wrap.addEventListener('mousemove', mousemoveHandler, false);
///////////////////////////////////
}
function resizeWindow() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
}
function mousemoveHandler(e) {
mouse.x = e.clientX;
mouse.y = e.clientY;
}
initHandlers();
initNodes();
redrawScene();
})();
html,
body {
margin: 0;
background-color: #25003a;
overflow: hidden;
}
canvas {
position: relative;
top: 0;
bottom: 0;
width: 100%;
height: 100%;
background-color: #25003a;
overflow: hidden;
}
#wrap {
position: absolute;
top: 0;
left: 0;
height: 100vh;
width: 100vw;
line-height: 100vh;
}
#wrap p {
text-align: center;
border: 1px solid;
}
#wrap p a {
color: white;
font-size: 70px;
text-decoration: none;
}
#wrap p a:hover {
text-decoration: underline;
}
<canvas></canvas>
<div id="wrap">
<p>
this is a link</p>
</div>

AS3 animate dynamically created movie clips with ENTER_FRAME

I have some code which loads a movie clip from the library, reproduces it and spreads it around the stage in different sizes, positions and rotations. What I can't figure out is how to then animate each one with an ENTER_FRAME event listener - So maybe I can also animate the scale, position and rotations? Any help greatly appreciated. Thanks.
for (var i = 0; i < 20; i++ )
{
//Generate Item from library
var MovieClip_mc:mcMovieClip = new mcMovieClip();
addChild(MovieClip_mc);
//Size
var RandomSize = (Math.random() * 0.5) + 0.5;
MovieClip_mc.scaleX = RandomSize;
MovieClip_mc.scaleY = RandomSize;
//Rotation
var RandomRotation:Number = Math.floor(Math.random()*360);
MovieClip_mc.rotation = RandomRotation;
//Position
MovieClip_mc.x = Math.floor(Math.random() * CanvasWidth);
MovieClip_mc.y = Math.floor(Math.random() * CanvasHeight);
}
For performance benefits, it is better to do it using one ENTER_FRAME handler. The handler can be located in your main class and update each mcMovieClip's properties by calling certain methods declared in the mcMovieClip class. Below is a simple example.
Main class
var mcs:Array = [];
for(var i:int = 0; i < 1; i++)
{
mcs.push(new mcMovieClip());
addChild(mcs[i]);
}
addEventListener(Event.ENTER_FRAME, updateTime);
function updateTime(e:Event):void
{
for(var j:int = 0; j < mcs.length; j++)
{
mcs[j].updatePosition(Math.random() * stage.stageWidth,
Math.random() * stage.stageHeight);
mcs[j].updateRotation(Math.random() * 360);
mcs[j].updateSize(Math.random());
}
}
mcMovieClip class
function updateSize(size:Number):void
{
this.scaleX = this.scaleY = size;
}
function updateRotation(rot:Number):void
{
this.rotation = rot;
}
function updatePosition(newX:Number, newY:Number):void
{
this.x = newX;
this.y = newY;
}
You don't actually need to do that from the outside. You can animate it with its own script in the first frame, so each instance will be animated:
addEventListener(Event.ENTER_FRAME, onFrame);
function onFrame(e:Event):void
{
// Math.random() - 0.5 will produce a random value from -0.5 to 0.5
x += Math.random() - 0.5;
y += Math.random() - 0.5;
scaleX += (Math.random() - 0.5) / 10;
scaleY = scaleX;
rotation += (Math.random() - 0.5) * 5;
}

Video display rotation position changed

I am trying to rotate video display depend on angle.
I got total 4 angle 0,-90,90,180.
And i make rotation as per following:
if(videoAngle == 180)
{
videoDisplay.rotation = 180;
}
else if(videoAngle == 90)
{
videoDisplay.rotation = -90;
}
else if(videoAngle == -90)
{
videoDisplay.rotation = 90;
}
It rotate the videoDisplay but it's x and y position is changed and also height and width.
I tried many things but didn't succeed.
Is there any solution for that?
Video object have rotation point fixed in top left, so you need to put this video in a container (MC or Sprite) with 'x' and 'y' in the center of video.
You can do it in fla but also in code:
function putInCenter(display:DisplayObject):Sprite {
var box:Sprite = new Sprite()
display.parent.addChild(box)
box.x = display.x + display.width / 2
box.y = display.y + display.height / 2
box.addChild(display)
display.x = -display.width / 2
display.y = -display.height / 2
return box
}
var videoBox:Sprite = putInCenter(videoDisplay)
Now you rotate this container instead of "videoDisplay"
if(videoAngle == 180)
{
videoBox.rotation = 180
}
else if(videoAngle == 90)
{
videoBox.rotation = -90;
}
else if(videoAngle == -90)
{
videoBox.rotation = 90;
}

Smoothly rotating a canvas element on setInterval()

I am trying to replicate this effect: http://codepen.io/kenjiSpecial/pen/enrbq
I have a Segment class:
function Segment (x, y, length) {
this.x = x;
this.y = y;
this.length = length;
this.rotation = 0;
this.color = '#fff';
}
I use it to fill the screen with vertical lines (made out of segments) and then rotate each of them by 45deg clockwise using this function and a setInterval:
function drawSegments(segment) {
segment.rotation = angle;
segment.draw(ctx);
}
setInterval(function(){
angle += 45 * Math.PI / 180;
}, 800);
It works (i.e. the lines are rotated on each 800ms) but there is no animation between the transitions.. I tried somethig like this, but unfortunately it doesn't work...
for (var i = 0; i < 360; i++) {
angle += i * Math.PI / 180;
}
So, the problem is - how to animate each rotation of the segments?
Here is the link to the live demo: http://codepen.io/gbnikolov/pen/KEGzJ

How to make an object move relative to the mouse pointer using actionscript 3? [duplicate]

This question already has answers here:
How to make my character move relative to the mouse position with actionscript 3?
(2 answers)
Closed 9 years ago.
I am making a game where I want my character to move towards the mouse pointer when pressing forward and for it to strafe left and right with the respecting arrow keys.
Here is my current code:
import flash.events.MouseEvent;
//Event Listners
stage.addChild(crosshair_mc);
crosshair_mc.mouseEnabled = false;
crosshair_mc.addEventListener(Event.ENTER_FRAME, fl_CustomMouseCursor);
function fl_CustomMouseCursor(event:Event)
{
crosshair_mc.x = stage.mouseX;
crosshair_mc.y = stage.mouseY;
}
Mouse.hide();
stage.addEventListener(MouseEvent.MOUSE_MOVE,facecursor);
stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_KeyboardDownHandler);
//Functions
function facecursor(event):void
{
character_mc.rotation = (180 * Math.atan2(mouseY - character_mc.y,mouseX - character_mc.x))/Math.PI + 90;
}
function fl_KeyboardDownHandler(event:KeyboardEvent):void
{
trace("Key Code Pressed: " + event.keyCode);
if (event.keyCode == 38)
{
character_mc.y = character_mc.y - 5;
}
if (event.keyCode == 40)
{
character_mc.y = character_mc.y + 5;
}
if (event.keyCode == 39)
{
character_mc.x = character_mc.x + 5;
}
if (event.keyCode == 37)
{
character_mc.x = character_mc.x - 5;
}
}
You have the rotation part right, now all you need is to incorporate that with cos and sin for the x and y axis respectively. Example:
var speed:Number = 8;
var angle:Number = Math.atan2(mouseY - character_mc.y, mouseX - character_mc.x);
character_mc.rotation = angle * 180 / Math.PI;
character_mc.x += Math.cos( angle ) * speed;
character_mc.y += Math.sin( angle ) * speed;
To avoid confusion, I would stop adding 90 degrees onto the rotation and instead rotate your graphics to face right / east.
The strafing thing uses the same logic, you just want to add a quarter of a circle to the rotation in whichever direction you want to strafe. FYI, a quater of a circle in radians is PI / 2. There are 2 PI radians in one circle:
// Augment angle for strafing.
angle += Math.PI / 2;