Unable to display anything through Canvas - html

I am trying to build a highlighting library with JavaScript and jQuery. I am just diving into Canvasing techniques this week and did not find them to be all that difficult. However, while working today my code has simply stopped working. I know I am probably just missing something obvious but I have been stuck like this for almost 2 hours now and I need to get this project moving forward again. any help would be greatly appreciated.
$(function() {
$('area').click(function(event) {
event.preventDefault();
document.getElementById("ctx").getContext("2d").fillStyle = "#FF0000";
document.getElementById("ctx").getContext("2d").fillRect(0, 0, 200, 200);
} );
} );
I have included my Javascript only since that is the only thing I have been changing recently.

Your code works for me, assuming:
The page has a clickable area.
The page has a canvas with an #id of ctx.
Make sure those 2 things are true about your setup...
Does your canvas element have an #id of ctx? That's not fatal, but the canvas element contains a context so it's a bit misleading.
If you have a canvas element like this:
<canvas id=canvas></canvas>
Then you can get a reusable reference to the canvas's context like this:
// no need to constantly get a context reference ...
// just do it once at the start of your app
var canvas=document.getElementById('canvas');
var context=canvas.getContext('2d');
And you can reuse that context reference to do all your drawing calls:
context.fillStyle='red';
context.fillRect(0,0,200,200);

Related

How can I add a sound effect to my buttons?

I want to make a sound effect for my buttons, so when the user clicks it, it goes 'ding'. I have a 1.6 second MP3 file I would like to play upon each click.
I have not started using java-script but everywhere I look, I need java-script to run this. I thought it might be able to be set up like background music.
I figure adding an example of my own code would not help here. I think an example of this will do, and I would be able to understand it reasonable clearly.
Thanks in advance!
This is not very hard. Here is a minimal example:
(Enjoy the Free Software Song)
var button = document.querySelector('button'),
audio = null;
button.addEventListener('click', handler, false);
function handler() {
audio = new Audio('https://www.gnu.org/music/FreeSWSong.ogg');
audio.play();
}
<button>Play</button>

How can I transfer code inside of a symbol in Flash to my document class?

I am pretty new to coding in general so I'll admit my understanding of inheritance is poor. I have this code that works when it's embedded in a symbol in my Fla doc, but I can't figure out how to transfer it to the Main document.
stop();
stage.focus = input_txt;
var outputText:String;
input_txt.visible = true;
input_txt.addEventListener(KeyboardEvent.KEY_DOWN, pressEnter);
function pressEnter(e:KeyboardEvent):void {
if(e.charCode == 13) {
captureText();
this.nextFrame();
}
}
function captureText():void {
outputText=input_txt.text;
}
All that code occurred on frame2 of the office_mc symbol, and then frame 3 has this:
output_txt.text = outputText;
I'm aware that I needed to reference the txt objects as office_mc.input_txt inside the main document since they are embedded. I'm also aware that outputText would need to be a global variable. But no matter how I tried to move things around I kept having an error due to something being null. For a little background information, I'm just trying to have user input displayed in a different area within this symbol.
Could someone explain or give an example of how to execute this code in a document class? Also I'm just totally confused about how to communicate between classes in general so if anyone could point me towards tutorials or example code that could help with that understanding, it'd be greatly appreciated. :)
Don't code in timeline at all (this is a good practice).
You should use only classes which eventually points to objects from the timeline.
Here you have some good tutorials:
How to Use a Document Class
AS3 OOP (good tutorial)

Play one of a set of audio clips randomly?

So I'm trying to setup a web-page where when you open it, it will play a random piece of music, and when that one finishes, it will play another directly after so you get a constant stream of music, but not in the same order every time. If this goes outside the bounds of HTML and I'm looking at for instance JavaScipt than that's fine.
I know this is probably a rather easy solution, but I'm new to HTML and trying to understand it better.
Thanks in advance if I don't get back to you soon!
This is best solved using JavaScript, which is used to add behavior to a website. HTML is primarily used to structure your site.
We can get a collection of all audio elements with the querySelectorAll function. Then we find a random element with the next line and call the play method.
var audio = document.querySelectorAll("audio");
function playRandom() {
audio[Math.floor(Math.random() * audio.length)].play()
}();
For the second part of your question, you would want to listen for the ended event. Inside the event listener function, you would then call the playRandom() function.
audio.addEventListener("ended", function() {
playRandom();
});

Html5 drag and drop unicode chessboard

I am trying to do html5 dragndrop on my html5/css chessboard using unicode pieces but have a problem at the moment with it.
On dragging & dropping the whole 'li' element seems to move & on drop this disrupts the board.
This can be seen on the chess WordPress site at:
http://www.buryknightschess.org.uk/play-chess/
(As can be seen I asked for help with this on SitePoint Forums but haven't had a reply to help yet from there).
Possibly I somehow need just to make the unicode chess pieces draggable & not the whole chess square (<li></li>).
I shall keep trying to fix this but meanwhile I would greatly appreciate any advice to help with this html5 dragndrop issue. I look forward to helpful replies, many thanks
The reason the <li/> is being moved (thus rearranging the chess board) is because in your drop function is calling appendChild:
function drop(event) {
var element = event.dataTransfer.getData("Text");
// This changes the DOM
event.target.appendChild(document.getElementById(element));
event.stopPropagation();
return false;
}
Instead of moving that actual DOM node, the drop function should copy the innerHTML of the node that's being dragged. Here's how the new drop would look function:
function drop(event) {
var coordinate = event.dataTransfer.getData("Text");
var element = document.getElementById(coordinate);
event.target.innerHTML = element.innerHTML; // Moving piece to new cell
element.innerHTML = ""; // Clearing old cell
event.stopPropagation(); // Consider using `event.preventDefault` instead
return false;
}
Also, I'd consider using event.preventDfault instead of event.stopPropagation. This makes it possible to add new drop listeners to the board or one of its parents. For example you may want to add a drop listener to the document body to handle the case where a user tries to drag a piece off the board.
As general remark, have you considering using jQueryUI draggable and droppable libraries? You'd be able to support much richer drag/drop interactions such as snap-to-grid movement and delayed starts. Also, HTML5 drag-and-drop is typically used to allow users to drag files from their OS desktop onto the a web page. The way your using it is totally fine, it's just a little strange.

How do i create a nice looking image fadein onload with mootools?

I´m creating a website for a photographer who would like a fine fadein on his images. I have excluded flash as a solution and would like to use those fine-looking effects of mootools fx. But the problem is that I'm really lousy when it comes to javascript. So if anyone could give me an easy solution for fading in one single image onload and when the image is loaded I would be really happy. I know there is a lot of different people out there who have this. But the problem is that i don't know how the code works even if it is a complete solution. So most important. If anyone has got the time to explain what every single line of code does i would be more than grateful. Thanks!
A simple fade-in is surely the simplest thing one can imagined:
// set-up an event on the browsers window
window.addEvents({
// be sure to fire the event once the document is fully loaded
load: function(){
// assing 'singleImage' variable to the image tag with the 'image' ID
var singleImage = $('image');
// set a bunch of CSS styles to the aforementioned image
singleImage.set('styles', {
'opacity': 0,
'visibility': 'visible'
});
// fade in the image
singleImage.fade('in');
}
});​
Working example: http://jsfiddle.net/oskar/RNeS5/ (HTML, CSS, MooTools)