views:

58

answers:

3

Hi all, basically what i want to do is this:

I have a flash game on a page (pacman for example)

I want to be able to take a print screen of that game using javascript

Is it possible? I know i can include the game swf in a nother swf and capture the screenshot using flash but i need it to be done in javascript. is this possible?

+1  A: 

Javascript cannot take a screen shot.

Prescott
+1  A: 

If you control both the host page and the flash movie, then you can generate the screenshot in flash, and simply send it to javaScript, and then do whatever you need with it in javaScript.

In ActionScript:

import flash.external.ExternalInterface;
ExternalInterface.call("showBase64Image", generateBase64Screenshot());
// see this Mario Klingenmann's file for ideas how to do that http://www.quasimondo.com/archives/000572.php

And then in javaScript:

function showBase64Image(base64data) {
    var img = document.createElement('img');
    img.src = base64data;
    document.body.appendChild(img);
}

P.S. don't forget to allowScriptAccess for embedded Flash, to allow Flash movie to access the javaScript on your page (which is definitely not wise if the Flash movie is made by a 3rd party).

On the other hand, if you do not control the flash movie (for instance if you wanna make a website that'll host 3rd party Flash games) then you're out of luck, and what you need is not doable with javaScript alone, and it can only be achieved through use of browser plugins/extensions.

ArtBIT
k i have 3rd party flash games... but they are stored on the same server as my website and are accessed by the same domain:gamesite.com/index.htmgamesite.com/3rdpartyflash.swfis it still possible like that?
Ozzy
Well @Ozzy, that's the danger, if you host 3rd party games on your site, and run them from your own domain, setting the `allowScriptAccess` parameter to `true` will basically allow the games to access (and change) your page data (url, cookies etc.), which is not good, cause malicious developers would take advantage of that definitely. Also, by controlling the flash movie I was thinking about having the source code of the movie and being able to modify it, i.e. add the code that will take the screenshot of the game and send it to javaScript via `ExternalInterface`.
ArtBIT
What exactly do you need this for @Ozzy? To generate game previews?
ArtBIT
yea Art, spot on. Im working on overhauling my arcade game website which has about 200 games on it. The old site doesnt have any previews and i was thinking instead of manually screenshoting each one and cropping etc... i could make a script that automates it by letting the game run for 10 seconds or w/e then take a snapshot and upload and move to the next game
Ozzy
maybe there is another way? via java or vbs? i will only be running the script once to update all old games, ill stick to manually previewing the new additions.
Ozzy
ok, how about this @Ozzy, use the link in my other answer to write a flash movie that will basically be a container able to load an external movie, in your case a game, but also be able to take screenshots. You would use actionScript's ExternalInterface class to allow javaScript to tell your movie to load the external movie/game, and also allow it to take a snapshot of the game after 10(or more seconds), and send it back to javaScript as base64 encoded image, which you can load into an `<img/>` element or send to server for processing or whatever. How does that sound to you?
ArtBIT
A: 

If it is your game and your website (in other words if you can modify both) try this:

http://danielmclaren.net/2008/03/use-javascript-to-take-a-screenshot-of-a-flash-movie

ArtBIT