easeljs hyperlink - javascript

Easeljs - Hyperlinking.
Out of an easeljs script: how can I hyperlink to another web site?
For instance: when an onClickEvent occurs another window would open - in my case I want to send a variable, an ID to another page with an SQL request on the ID for more details.
Any sample would be great.

When you click on the red shape it will open google.com. You can add the hyperlink any easeljs object like this.
<!DOCTYPE HTML>
<html>
<head>
<title></title>
<style>
#myCanvas{
background-color:#CCC;
}
</style>
<script src="easeljs-0.7.1.min.js"></script>
<script src="jquery-1.7.2.min.js"></script>
<script>
var canvas, stage;
$().ready(function(e) {
canvas = document.getElementById("myCanvas");
stage = new createjs.Stage(canvas);
var shape = new createjs.Shape();
shape.graphics.beginFill("#ff0000").drawRect(0, 0, 100, 100);
stage.addChild(shape);
shape.x = 200, shape.y = 100;
shape.addEventListener("click", goURL, false);
stage.update();
});
function goURL(){
window.open("http://google.com");
}
</script>
</head>
<body onload="init()">
<canvas id="myCanvas" width="500" height="400">
</canvas>
</body>
</html>

http://www.w3schools.com/jsref/met_win_open.asp
window.open(URL,name,specs,replace);
But be aware that popups/new tabs like this might be blocked by the browser if not followed an user-event. You could also retrieve the details via jQuery.get or jQuery.ajax ect.. and then display the information directly in your application.

You can also use window.location in case you only want to redirect to another page instead of open a new window/tab. For example:
var id = 0 //the variable you want to pass;
shape.addEventListener("click", gotoURL);
function gotoURL(){
location.assign( 'http://yoursite.com/search?id='+id );
}

Related

compare 2 canvas elements for similarity and return result

I'm new to js and html5 so here's what I'm doing : I'm working on a game that helps in teaching illustrator shortcuts, the firts level consist of 2 canvas one with an already existing image and the other blank and ready for user to draw on, on ctrl + s press(sure I disabled it's default action using jquery) I want to compare the content of those 2 canvas elements. I've found Image similarity api from deepai.org very useful and accurate, but it only accepts url or input="file" content, so I'm trying to find a way to upload (maybe) the drawn canvas as an image to a server and get the url like this : https://server.com/myaccount/images/img1.png and since i only upload one image I can pass that static url to the api in addition to the original image which will also have a static url so hopefully it compares.
I made a solution that works without a server. But I couldn't make it work in an online code repo like jsfiddle. So I put it on my own server for you to check. http://paulyro.com/paul/deepai/
For convenience I put everything in one file. Of course it would make sense to save the JS in a separate file. But I let that up to you.
For explanation: the red square in a black frame is the canvas. I generate two images and add them to the page. Then I send those images to the deepAI server when you press the button. You will only need one generated img, but for testing purpose I made 2.
Let me know if this is what you were looking for. Of course I expect you to adapt this solution to your exact needs ;)
This is the code:
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>DeepAiDemo</title>
<script src="https://cdnjs.deepai.org/deepai.min.js"></script>
<style rel="stylesheet">
canvas {
border: 1px solid black;
}
</style>
</head>
<body>
<canvas id="canvas1" width="200" height="200"></canvas>
<button name="button" onClick="load()">Press me</button>
<div style="position:absolute;left:400px; top:30px; height: 354px;" id="messages">Result will get here</div>
<script type="text/javascript">
(async function() {
var can = document.getElementById('canvas1');
var ctx = can.getContext('2d');
// create green canvas and make it an image
ctx.fillStyle = "green";
ctx.fillRect(75, 75, 50, 50);
var img = new Image();
img.src = can.toDataURL();
document.body.appendChild(img);
// create red canvas and make it an image
ctx.fillStyle = "red";
ctx.fillRect(75, 75, 50, 50);
var img2 = new Image();
img2.src = can.toDataURL();
document.body.appendChild(img2);
})()
const load = async () => {
document.getElementById('messages').innerHTML = "Waitng for response...";
deepai.setApiKey('xxxxxxxxxxxxxx');
var images = document.getElementsByTagName('img');
console.log("amount images: "+images.length);
console.log(images[0]);
console.log(images[1]);
var resp = await deepai.callStandardApi("image-similarity", {
image1: images[0],
image2: images[1],
});
console.log("response: ");
console.log(resp);
document.getElementById('messages').innerHTML = "Distance: " + resp.output.distance; //resp.output.distance contains the number from the server.
};
</script>
</body>
</html>
Cheers,
Paul

Brackets doesn't load my canvas

When I'm referencing my JavaScript file to my index.html file (two separate files in the same folder), and I load the html file in a browser, the drawing i made in the javascript file doesnt show in the canvas.
Here is this HTML code:
<!DOCTYPE html>
<html>
<head>
<title>snake</title>
<script language="javascript" type="text/javascript" src="snake.js"></script>
</head>
<body>
<canvas id="dots" height="500" width="500"></canvas>
</body>
</html>
and here is the JavaScript:
function snake() {
var canvas = document.getElementById("dots");
var snakehead = canvas.getContext('2d');
snakehead.beginPath();
snakehead.arc(100, 75, 50, 0, 2 * Math.PI);
snakehead.stroke();
snakehead.fillStyle = "#2776ff";
snakehead.fill();
}
snake();
in JS-fiddle the code works, it looks like this: https://jsfiddle.net/j2ny6gaf/72/
I'm also getting some errors from JSLint.
"Missing use of 'strict' statement, on the var canvas = document.getElementById
"Combine this with the previous var statement var snake = canvasgetcontext
and a ERROR;
"Document is not defined.[no-undef]
Try adding your script inside the head section (script src=“file-name.js” /script) and try adding a canvas size.
Try
window.onload = function snake() {...}
Try using the script tag
<script type="text/javascript" src="snake.js"></script>
Inside the head section.

Functions With Easel JS

I am trying (and failing) miserably with Easel JS and Canvas. I'm a bit new to canvas in general and learning JS as well. I am basically trying to make a canvas that I can update with colors, that are passed through a button click.
I have made a Fiddle to show my work so far.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>IllustratorSetup</title>
<script src="https://code.createjs.com/easeljs-0.8.2.min.js"></script>
<script>
var stage = new createjs.Stage("canvas");
var circle = new createjs.Shape();
function drawRectangle(){
var rect = new createjs.Shape();
rect.graphics.beginFill("#000").drawRect(10, 10, 80, 80);
stage.addChild(rect);
stage.update();
}
function drawShapes(){
drawRectangle();
stage.update();
}
</script>
</head>
<body onload="drawShapes()">
<canvas id="canvas" width="500" height="300"></canvas>
<button onclick="drawRectangle('#ff0000')">Click me for red</button>
</body>
</html>
Your demo likely doesn't work because you are defining your stage before the body onload, and passing it the ID of your canvas (which uses a document.getElementById under the hood). The canvas element won't be available until it is created in the body.
I recommend moving your code that isn't in a function to an init(), or even put it into the drawShapes method.
var stage, circle; // Keep them global
function drawRectangle(){
var rect = new createjs.Shape();
rect.graphics.beginFill("#000").drawRect(10, 10, 80, 80);
stage.addChild(rect);
//stage.update(); // Unnecessary, since it is called in drawShapes.
}
function drawShapes(){
stage = new createjs.Stage("canvas");
circle = new createjs.Shape();
drawRectangle();
stage.update();
}
You can also define your scripts in the body, after the canvas element, so that they initialize after the element is ready.
Note that your fiddle doesn't have the createjs libraries in it, and that JSFiddle will fire the code part of the fiddle during the onload event, so you may not see the issue you are having locally.

How can I remove a background image or color from Flash-exported HTML and JS

I've done some animation using Adobe Flash CC, and then I exported it in HTML and JS, but I can't find the way to remove the background.
I see the function and all, but I don't know how to remove it.
Here is the html:
<html>
<head>
<script src="http://code.createjs.com/easeljs-0.8.1.min.js"></script>
<script src="http://code.createjs.com/tweenjs-0.6.1.min.js"></script>
<script src="http://code.createjs.com/movieclip-0.8.1.min.js"></script>
<script src="http://code.createjs.com/preloadjs-0.6.1.min.js"></script>
<script src="Anime_V3.js"></script>
<script>
var canvas, stage, exportRoot;
function init() {
canvas = document.getElementById("canvas");
images = images||{};
ss = ss||{};
var loader = new createjs.LoadQueue(false);
loader.addEventListener("fileload", handleFileLoad);
loader.addEventListener("complete", handleComplete);
loader.loadFile({src:"images/Anime_V3_atlas_.json", type:"spritesheet", id:"Anime_V3_atlas_"}, true);
loader.loadManifest(lib.properties.manifest);
}
function handleFileLoad(evt) {
if (evt.item.type == "image") { images[evt.item.id] = evt.result; }
}
function handleComplete(evt) {
var queue = evt.target;
ss["Anime_V3_atlas_"] = queue.getResult("Anime_V3_atlas_");
exportRoot = new lib.Anime_V3();
stage = new createjs.Stage(canvas);
stage.addChild(exportRoot);
stage.update();
createjs.Ticker.setFPS(lib.properties.fps);
createjs.Ticker.addEventListener("tick", stage);
}
</script>
</head>
<body onload="init();" style="background-color:#D4D4D4">
<canvas id="canvas" width="1340" height="605" style="background-color:#FFFFFF"></canvas>
</body>
</html>
And I'm sorry for this, but the JS is just to big to fit in here, so I put it on pastebin, here is the link:
http://pastebin.com/fvqpMChM
I found a work around that allows you to make your canvas export with a transparent background. You can add this little piece of code to frame one in your actual flash file:
document.getElementById("canvas").style.backgroundColor = "rgba(255,255,255,0)";
What this does is target the canvas element when the piece is loaded by your user, and then change the background of the canvas piece from what ever your stage colour was to a fully transparent white. This should work properly in all browsers that support canvas, with the added bonus of not needing to change your file every time you export.
The background is added to the canvas as an inline style, based on the background color of your FLA. You can just remove the attribute.
<canvas id="canvas" width="1340" height="605" style="background-color:#FFFFFF"></canvas>
<canvas id="canvas" width="1340" height="605"></canvas>
Note that if you re-publish/export the content, it will overwrite your html, so you might want to turn that off in the publish settings.

Adding a simple image in easeljs

This is my htmlcode:
<!DOCTYPE html>
<html>
<head>
<title>test</title>
<script src="http://code.createjs.com/easeljs-0.7.0.min.js"></script>
<script src="Doge.js"></script>
</head>
<body bgcolor="#C7C7C7" onload="Start();">
<canvas id="DogeCanvas" width="480" height="320"></canvas>
</body>
</html>
And this is my Doge.js code:
function Start() {
var stage = new createjs.Stage("DogeCanvas");
var doge = new Image();
doge.src = "images/doge.jpg"
var bitmap = new createjs.Bitmap(doge);
stage.addChild(bitmap);
stage.update();
}
Why it doesn't show anything on the screen?
What is wrong?
The image is not loaded when the stage is updated. I posted an answer here:
» easeljs not showing bitmap
You can add a Ticker to the stage to constantly update it (which most applications do, since there is other things changing over time)
Listen for the onload of the image, and update the stage again
Preload the image with something like PreloadJS before you draw it to the stage.
As stated above you cannot draw your image until you have loaded it. Try this:
<!DOCTYPE HTML>
<html>
<title>Easel Test</title>
<head>
<script src="https://code.createjs.com/easeljs-0.8.0.min.js"></script>
<script>
var stage;
function init() {
stage = new createjs.Stage("myCanvas");
var image = new Image();
image.src = "path/image";
image.onload = handleImageLoad;
}
function handleImageLoad(event) {
var image = event.target;
var bitmap = new createjs.Bitmap(image);
stage.addChild(bitmap);
stage.update();
}
</script>
</head>
<body onload="init();">
<canvas id="myCanvas" width="960" height="580"></canvas>
</body>
</html>

Categories

Resources