I am working through a book called "Beginning Facebook Game Apps Dev" and in Chapter 3 they introduce us to create code for html document.
I'm on a Mac and I use TextEdit to create the pages. At first the book gave code to draw a rectangle and a triangle. Both were fine but then I try the code rotation, it only displays the box.
I have been programming for some time now with c++ without being an expert but I know absolutely nothing yet on how the internet works. Anyway the only difference I can see between the code that works and the one that doesn't is the fact to rotate I call functions where for the rectangle I dont. Could anyone tell me what is going on please?
ps: I tried to google html 5 function not working but honestly I'm at a loss for words on how to search for this specific problem.
thank you
code working (draw rectangle)
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>A simple Canvas Square</title>
<style type="text/css" media="screen">
#canvas
{
border: 1px solid #c0c0c0;
}
</style>
</head>
<body>
<canvas id="canvas" width="270" height="270"></canvas>
<script type="text/javascript" charset="utf-8">
var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');
context.fillStyle = "gray";
context.fillRect(30, 30, 200, 200);
</script>
</body>
</html>
code not working (rotation)
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>overlapping box</title>
<style type="text/css" media="screen">
#canvas
{
border: 1px solid #c0c0c0;
}
</style>
</head>
<body>
<canvas id="canvas" width="500" height="400"></canvas>
<script type="text/javascript" charset="utf-8">
function draw(canvas Id)
{
"use strict";
var canvas = document.getElementById(canvasId);
var context = canvas.getContext("2d");
context.fillStyle = "#091F5D";
context.fillRect(10, 10, 160, 150);
context.fillStyle = "rgba(255, 78, 0, 0.5)";
context.fillRect(90, 90, 160, 150);
context.fillStyle = "rgb(255, 78, 0)";
context.fillRect(170, 160, 160, 150);
}
draw('canvas');
</script>
</body>
</html>
in this line:
function draw(canvas Id)
you should not have a space between the words 'canvas' and 'Id'. Change it to:
function draw(canvasId)
canvasId is the name of the parameter (with the value passed being the id of the canvas element in which you want to draw the rectangles).
Related
I have created an image drawing canvas but the drawing color can't change. maybe it's javascript set color but can't find. if anyone knows please help me. this is the demo link
http://affibe.com/erevo/
HTML Code:
`
<html class="gr__adsterra_com">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script type="text/javascript" src="js/jquery-2.2.4.min.js"></script>
<script type="text/javascript" src="js/pixi.min.js"></script>
<script type="text/javascript" src="js/animationFrame.js"></script>
<script type="text/javascript" src="js/nodes.js"></script>
<style>body{margin: 0; background-color: transparent;}</style>
</head>
<body data-gr-c-s-loaded="true">
<header>
<script>lowPerformance=function (){var ua=navigator.userAgent.toLowerCase(); if (ua.indexOf("iphone") !=-1 || ua.indexOf("mobile") !=-1) return true; if ($(window).width()==0) return; return ($(window).width() <=480) ? true : false;}; var ie=false; var w=window, d=document, documentElement=d.documentElement, body=d.getElementsByTagName('body')[0], height=w.innerHeight|| documentElement.clientHeight|| body.clientHeight; if (navigator.appName=='Microsoft Internet Explorer') ie=true; Nodes.multipleInit([{"post_name": "AFFIBE", "drawnImage": (height>860)?"http:\/\/affibe.com\/erevo\/images\/main\/logo_header_er.png":"http:\/\/affibe.com\/erevo\/images\/main\/logo_header_er.png", "linkTitle": "AFFIBE", "particleDensity": ie?"11":"7", "particleWidth": "0.5", "particleHeight": "0.5"}]); </script>
</header>
</body>
</html>
pixi.min.js:
http://affibe.com/erevo/js/pixi.min.js
animationFrame.js
!function(){for(var n=0,i=["ms","moz","webkit","o"],e=0;e<i.length&&!window.requestAnimationFrame;++e)window.requestAnimationFrame=window[i[e]+"RequestAnimationFrame"],window.cancelAnimationFrame=window[i[e]+"CancelAnimationFrame"]||window[i[e]+"CancelRequestAnimationFrame"];window.requestAnimationFrame||(window.requestAnimationFrame=function(i){var e=(new Date).getTime(),a=Math.max(0,16-(e-n)),o=window.setTimeout(function(){i(e+a)},a);return n=e+a,o}),window.cancelAnimationFrame||(window.cancelAnimationFrame=function(n){clearTimeout(n)})}();
if i understand it the follow code help to you
var c = document.getElementById("canvas");
var ctx= c.getContext("2d");
ctx.fillStyle="#66bfff";
for example
var c = document.getElementById('canvas');
var ctx = c.getContext("2d");
ctx.fillStyle = "#f00";
ctx.fillRect(40, 40, 40, 40);
#canvas{
border: 2px solid #333;
}
<html>
<body>
<canvas id="canvas" width="100" height="100"></canvas>
</body>
</html>
I am doing this small project - my cursor moves in a left rectangle and it should appear in the second one. I have made an image of cursor and I am trying to move it according to my cursor, but nothing happens. Could you help me to find the mistake? I would be very grateful!
Here is the code snippet:
<html>
<head>
<title>Laboratory work 1_4</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
#imgc{
position: absolute;
}
</style>
</head>
<body>
<img src="arrow.png" id="imgc" width="20" height="20">
<canvas id="canvas" width="800" height="600" ></canvas>
<script type="text/javascript">
var c = document.getElementById("canvas");
var ctx = c.getContext("2d");
ctx.rect(20, 20, 300, 200);
ctx.stroke();
ctx.rect(350, 20, 300, 200);
ctx.stroke();
</script>
<script type="text/javascript" src="js/libs/jquery/jquery.js">
$("#canvas").mousemove(function(e){
$("#imgc").stop().animate({left:e.pageX, top:e.pageY});
});
</script>
</body>
P.S. I added jquery library in a JavaScript libraries wizard in Netbeans. So the structure of my project looks like this:
A simple problem to most of you however I have an issue with my HTML or JavaScript. I am trying to make some rectangles appear when running the code. I am met with the all to familiar blank screen.
Here is the HTML:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="../css/myCanvas.css"/>
<script type="text/javascript" src="../js/rectangles.js">
</script>
<meta charset="UTF-8">
<title>Canvas Example</title>
</head>
<body>
<canvas id="lessonCanvas" width="300" height="300" style="margin:100px;">
<p>This example requires a browser that supports the HTML5 canvas feature.</p>
</canvas>
</body>
</html>
And the JavaScript:
function setupCanvas() {
var canvas = document.getElementById('lessonCanvas');
if (canvas.getContext) {
var ctx = canvas.getContext('2d');
ctx.strokeRect(0, 0, 200, 200);
ctx.strokeStyle = 'rgb(255, 0, 0)';
ctx.strokeRect (0.5, 0.5, 100, 100);
ctx.fillRect (20, 20, 100, 100);
ctx.fillStyle = 'rgb(0, 255, 0)'
ctx.fillRect (50, 50, 100, 100);
ctx.clearRect (80, 80, 30, 30);
}
}
Any help for a clueless would be appreciated
You have to actually call setupCanvas
Here's a codepen showing this. As you can see, I just added one line to your code. I hope this is the result you intended
in specification of FFOS GP PEAK is written, that it's got qHD display (it is 960*540), but when I run JavaScript code:
console.log(screen.width)
console.log(screen.height)
I get 640*360. Is it JavaScript bug? Or anything else?
Thank you.
I believe the Peak has a device pixel ratio of 1.5, which would be 640x360 logical pixels.
You may want to have a look at css - what exactly is device pixel ratio
and Bug 838505
If I use the following HTML and JS this draws a square around the entire screen.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<!--<meta name = "viewport" content="user-scalable = no"> -->
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<link rel="stylesheet" type="text/css" href="css/background.css">
<title>Test</title>
<script type="text/javascript" src="js/loop.js"></script>
<style type="text/css">
*
{
border: 0px;
margin: 0px;
padding: 0px;
}
</style>
</head>
<body><canvas id="myCanvas"></canvas></body>
</html>
loop.js
//Main file for game logic
window.onload = init;
//Setup function to reset start location
function setup() {
canvas = document.getElementById('myCanvas');
canvas.height = window.innerHeight;
canvas.width = window.innerWidth;
context = canvas.getContext('2d');
context.beginPath();
context.lineWidth="6";
context.strokeStyle="red";
context.rect(0,0,canvas.width,canvas.height);
context.stroke();
}
//Initialize game and event handlers
function init() {
setup();
}
I am trying to draw a red rectangle on my HTML5 canvas. Here is my HTML.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset=utf-8>
<title>My Canvas Experiment</title>
<link rel="stylesheet" type="text/css" href="main.css" />
<script src="plotting.js"></script>
<!--[if IE]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js">
</script>
<![endif]-->
</head>
<body>
<canvas id="plot"></canvas>
</body>
</html>
Here is plotting.js:
document.onload = function() {
var c = document.getElementById("plot");
var ctx = c.getContext("2d");
ctx.fillStyle("#f00");
ctx.fillRect(0, 0, 175, 40);
}
Here is main.css:
body { margin:100px; }
article, aside, figure, footer, header, hgroup, menu, nav, section {
display:block;
}
#plot {
width: 500px;
height: 400px;
}
Why is the page blank? Chrome Web Developer Console issues no errors.
Replace:
ctx.fillStyle("#f00");
with:
ctx.fillStyle="#f00";
Use window.onload instead of document.onload, (keeping #stewe's suggestion).
DEMO
Replacectx.fillStyle('#f00'); to ctx.fillStyle = 'red'; because fillStyle is a variable and NOT the function.
I've been having issues all morning with javascript not drawing my shapes. Turns out you HAVE to set the width and height on the canvas tag in HTML, and not through the CSS, or the shapes will not draw.
example:
<canvas id="map" width="500" height="500"></canvas>