Scrolling camera in HTML5 canvas game (with javascript) - javascript

I have a simple functioning 2d HTML canvas game, at the moment you can see the whole map, I want there to be a scrolling camera for the canvas game so you can't see the entire map at once, I have no idea how to do this. I've Googled a bit, found nothing.
ctx.drawImage(character,x,y);
canvas already correctly setUp
<canvas id="canvas"></canvas>
There’s no errors or bugs

2 way :
1) adjust the positon of the object to draw with the camera offset:
deltaX=object.x-cameraX
deltaY=object.y-cameraY
if(deltaX + object.width > 0
&& deltaX - object.width < cameraWidth
&&deltaY + object.height > 0
&& deltaY - object.height < cameraHeight){
ctx.drawImage(character,deltaX,deltaY);
}
2) have two context and print one into the other
ctxCamera.drawImage(ctx,cameraX,cameraY);

I don't know if I'm too late, but you can encompass the canvas tags with div tags.
HTML:
<div id = "viewport">
<canvas id="canvas"></canvas>
</div>
CSS:
#viewport{
overflow: hidden; //so you can't see outside of the div
width: /*insert desired amount*/px;
height: /*insert desired amount*/px;
}
Javascript:
function scrollCamera(camx, camy){
var viewport = document.getElementById('viewport');
viewport.scrollTop = camy;
viewport.scrollLeft = camx;
}
scrollCamera(x-/*desired amount*/, y-/*desired amount*/); //makes the camera follow the player

Related

Cursor visual effect

I need help,
I just saw this main page on a great website www.snapplespiked.ca.
I really like so much this kind of cursor-waving effect, when you moving the cursor all over the page. I was trying this web on Atom/VsCode/Codepen and CAN NOT get these effects on the whole page. It appears only in first place on the main page when you just entering on website. In the moment when you scrolling a page down to see the rest of the parts on the page the effect disappears, not working.
I use the same HTML and CSS and all js scripts and there is not the same effect as on the original website.
What I did wrong?
Is anyone know what I did wrong ?
This effect come from this part of the code in HTML:
<div class="c-background js-app-background">
<div class="c-background__texture js-app-backgroundTexture">
<div data-product-id="795" style="--url: url(https://www.snapplespiked.ca/wp-content/uploads/2021/02/mango.png);" data-color="#fc9a44" class=""></div>
<div data-product-id="804" style="--url: url(https://www.snapplespiked.ca/wp-content/uploads/2021/02/lemon_peel.png);" data-color="#ffe24a" class=""></div>
<div data-product-id="808" style="--url: url(https://www.snapplespiked.ca/wp-content/uploads/2021/02/watermelon.png);" data-color="#4ca463" class="is-active"></div>
<div data-product-id="798" style="--url: url(https://www.snapplespiked.ca/wp-content/uploads/2021/02/raspberry.png);" data-color="#ff3443" class=""></div>
<div data-product-id="806" style="--url: url(https://www.snapplespiked.ca/wp-content/uploads/2021/02/strawberry.png);" data-color="#ff3443" class=""></div>
<div data-product-id="802" style="--url: url(https://www.snapplespiked.ca/wp-content/uploads/2021/02/lemon.png);" data-color="#ffe24a" class=""></div>
<div data-product-id="800" style="--url: url(https://www.snapplespiked.ca/wp-content/uploads/2021/02/peach.png);" data-color="#fc9a44" class=""></div>
</div>
<canvas class="c-background__canvas js-app-backgroundCanvas" style="width: 784px; height: 779px;" width="1176" height="1168"></canvas>
</div>
<div class="c-background c-background--foreground js-app-background">
<div class="c-background__texture js-app-backgroundTexture">
<div data-product-id="795" style="--url: url(https://www.snapplespiked.ca/wp-content/uploads/2021/02/mango.png);" data-color="#fc9a44" class="is-active"></div>
<div data-product-id="804" style="--url: url(https://www.snapplespiked.ca/wp-content/uploads/2021/02/lemon_peel.png);" data-color="#fc9a44" class=""></div>
<div data-product-id="808" style="--url: url(https://www.snapplespiked.ca/wp-content/uploads/2021/02/watermelon.png);" data-color="#fc9a44" class=""></div>
<div data-product-id="798" style="--url: url(https://www.snapplespiked.ca/wp-content/uploads/2021/02/raspberry.png);" data-color="#fc9a44" class=""></div>
<div data-product-id="806" style="--url: url(https://www.snapplespiked.ca/wp-content/uploads/2021/02/strawberry.png);" data-color="#fc9a44" class=""></div>
<div data-product-id="802" style="--url: url(https://www.snapplespiked.ca/wp-content/uploads/2021/02/lemon.png);" data-color="#fc9a44" class=""></div>
<div data-product-id="800" style="--url: url(https://www.snapplespiked.ca/wp-content/uploads/2021/02/peach.png);" data-color="#fc9a44" class=""></div>
</div>
</div>
You shouldn't directly copy code snippets from other websites, as you don't have the license to use them legally, and they're only really guarrenteed to be fit for their original purpose --- as you've found, it doesn't really work for your use case, and often it'll also result in a lot of redundant code.
In fact, this effect isn't particularlly hard, so lets try to re-create it ourselves!
Getting Started
The HTML5 canvas element lets us draw pixels onto it in various shapes. It's really useful for creating complex graphical effects like this one. The background of the canvas is transparent by default, and you can clear pixels off of it, so one way we could do this would be to fill the entire canvas white every frame, and then erase away the white where the cursor trails are to reveal the background underneath.
This means that we need to do a few things in our JavaScript:
Keep track of the user's mouse movements over the canvas, and an initial size property of the blob, which we'll reduce over time.
Every tick...
Fill the canvas with white
Loop over our list of blobs that we're keeping track of
For each one, erase a circle from the canvas centered from the mouse's position at that time and with a radius equal to the size that we're also keeping track of.
Decrement the size by some number so that next tick we draw the blob slightly smaller.
If the size is less than some number (zero, say), then stop tracking that blob anymore
Getting the <canvas> in place
We need to start by making the canvas and placing it on the page. Almost everything else will be done with JS.
<div id="page">
<canvas id="mouseTrailEffect"></canvas>
<div class="content"></div>
</div>
The page is the container for both the canvas and the content. The content div contains all of the other content which should appear over the top of the canvas.
We want to do a couple things with the CSS here too:
#page {
position: relative;
}
#page canvas {
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
}
#page .content {
position: absolute;
}
This makes sure that it fills its entire parent, but remains sorted behind the rest of the content.
In the JavaScript, we now need to create a reference to the canvas, it's drawing context, and we also need to adjust its size, so that the width and height are set explictly --- even though this has been done with CSS, this will actually just stretch the canvas's pixels to cover the requested area, rather than adding more pixels to it to match the space it covers 1-to-1, which results in it looking quite blurry. We also need to make sure we adjust its size again whenever the user resizes the window, to make it responsive.
function resizeCanvas(cv) {
let rect = cv.getBoundingClientRect();
cv.width = rect.width;
cv.height = rect.height;
}
function setupMouseTrailEffect() {
let cv = document.getElementById("mouseTrailEffect");
let ctx = cv.getContext("2d");
resizeCanvas(cv);
window.addEventListener("resize", ()=>{
resizeCanvas(cv);
});
}
window.addEventListener("load", ()=>{ // When the page load is completed
setupMouseTrailEffect();
});
Keeping track of the mouse movements
Whenever the user moves their cursor over our canvas, we get a mousemove event fired on it that we can listen to. We can then extract the X and Y positions of the mouse from that event (relative to the page's scroll position and the canvas's offset from the top-left corner of the page), and create an object like to represent the blob.
{x: ____, y: ____, size: ____}
We can add those objects into an array, which I'll call blobs. One thing to note here is that we actually listen for the mousemove event on the canvas's parent, not the canvas itself. This is so that it gets called even if the user hovers over something else on the page (e.g. some content) which isn't directly the canvas itself.
let blobs = [];
const INITIAL_BLOB_SIZE = 50; // This is radius that blobs will be when they start out! You can change it to make them bigger / smaller.
document.getElementById("page").addEventListener("mousemove", (ev)=>{
blobs.push({
x: ev.pageX - cv.offsetLeft,
y: ev.pageY - cv.offsetTop,
size: INITIAL_BLOB_SIZE
});
});
Updating every tick
Now, we need to figure out how to update our canvas every tick. This can be done by first defining the function which should be run each tick, and then at the end of it telling the browser that the next animation frame it gets it should try and run our function again.
const tick = ()=>{
ctx.clearRect(0, 0, cv.width, cv.height);
ctx.fillStyle = "#ffffff"; // white
ctx.fillRect(0, 0, cv.width, cv.height); // fill the entire canvas with white
window.requestAnimationFrame(tick); // run this again ASAP
}
tick(); // Run the first tick to get it started!
Drawing the blobs
Now for the fun part!
Inside our tick function, after we fill everything white, lets loop through our blobs and erase them from the canvas. Note that the canvas works like e.g. Microsoft Paint does, in that there are no layers --- every pixel can have exactly one colour at one time. Filling it white sets them all to white, and to erase the area occupied by a blob we'll just draw a new shape there, and composite it so that it gets removed and the pixels are set to a fully transparent colour.
We also need to make sure that we adjust the size of the blobs each tick, and that we can remove them from the array once they reach a set minimum size. To do the latter, we'll make sure we loop through the array backwards, so that deleting one doesn't change the indexes that we're looping over.
const BLOB_DECAY_RATE = 2; // How many pixels to remove from the size of a blob each tick
const MIN_BLOB_SIZE = 0; // When a blob reaches this size, we stop tracking it and it disappears.
ctx.globalCompositeOperation = "destination-out"; // Instead of drawing these shapes, erase them instead!
for (let i = blobs.length - 1; i >= 0; i--) { // Loop over the list of blobs backwards
ctx.beginPath(); // Create a path which we'll erase
ctx.arc(blobs[i].x, blobs[i].y, blobs[i].size, 0, Math.PI * 2); // Add a arc to the path with is a circle, centered at the blob's X and Y positions, and with its size as the radius.
ctx.fill();
blobs[i].size -= BLOB_DECAY_RATE; // Adjust the blob's size
if (blobs[i].size <= MIN_BLOB_SIZE) {
blobs.splice(i, 1); // Remove the blob from the array
}
}
ctx.globalCompositeOperation = "source-out"; // Stop erasing when we draw
All together now
Here's all that code put together into a working example!
const INITIAL_BLOB_SIZE = 50; // This is radius that blobs will be when they start out! You can change it to make them bigger / smaller.
const BLOB_DECAY_RATE = 4; // How many pixels to remove from the size of a blob each tick
const MIN_BLOB_SIZE = 20; // When a blob reaches this size, we stop tracking it and it disappears.
function resizeCanvas(cv) {
let rect = cv.getBoundingClientRect();
cv.width = rect.width;
cv.height = rect.height;
}
function setupMouseTrailEffect() {
let cv = document.getElementById("mouseTrailEffect");
let ctx = cv.getContext("2d");
resizeCanvas(cv);
window.addEventListener("resize", ()=>{
resizeCanvas(cv);
});
let blobs = [];
document.getElementById("page").addEventListener("mousemove", (ev)=>{
blobs.push({
x: ev.pageX - cv.offsetLeft,
y: ev.pageY - cv.offsetTop,
size: INITIAL_BLOB_SIZE
});
});
const tick = ()=>{
ctx.globalCompositeOperation = "source-out"; // Stop erasing when we draw
ctx.clearRect(0, 0, cv.width, cv.height);
ctx.fillStyle = "#fff"; // white
ctx.fillRect(0, 0, cv.width, cv.height); // fill the entire canvas with white
ctx.globalCompositeOperation = "destination-out"; // Instead of drawing these shapes, erase them instead!
for (let i = blobs.length - 1; i >= 0; i--) { // Loop over the list of blobs backwards
ctx.beginPath(); // Create a path which we'll erase
ctx.arc(blobs[i].x, blobs[i].y, blobs[i].size, 0, Math.PI * 2); // Add a arc to the path with is a circle, centered at the blob's X and Y positions, and with its size as the radius.
ctx.fill();
blobs[i].size -= BLOB_DECAY_RATE; // Adjust the blob's size
if (blobs[i].size <= MIN_BLOB_SIZE) {
blobs.splice(i, 1); // Remove the blob from the array
}
}
window.requestAnimationFrame(tick); // Run this again ASAP!
}
tick(); // Run the first tick to get it started!
}
window.addEventListener("load", ()=>{ // When the page load is completed
setupMouseTrailEffect();
});
#page {
/** Gradient just so we can see the effect **/
background-image: linear-gradient(0deg, #f00 0%, #ff0 100%);
border: solid 1px #000;
height: 250px;
position: relative;
padding: 10px;
}
#page canvas {
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
}
#page .content {
position: absolute;
}
<div id="page">
<canvas id="mouseTrailEffect"></canvas>
<div class="content">
<h1>Some content</h1>
Stuff above the canvas.
</div>
</div>
You can tweak the three constants, INITIAL_BLOB_SIZE, BLOB_DECAY_RATE and MIN_BLOB_SIZE to make the effect closer to what you want (and I've moved them all together to the top of the snippet to make that easy :D).

How to export Canva Render has GIF with CSS modification

I'm using p5.js to make a GIF animation but I have an issue when I want to export it.
I did a pixelased effect on my animation by adding these css properties to the Canva (140*140) :
image-rendering: pixelated;
width:500px;
height:500px;
My problem is that I can't export this Canva with the properties I added. (I'm using CCapture)
My gif is in 140x140 without the pixelated effect.
How can I get the rendering I need?
There is a difference between the width & height you can set for a HTML element e.g. <canvas width='140' height='140'> and the CSS width & height properties.
The former defines the actual size of the canvas - 140x 140in this case.
If we now set the CSS width & height to something deviating from the HTML element's width & height e.g. <canvas width='140' height='140' style='width: 500px; height:500px;'> the actual size in pixels of the canvas does not change - it stays 140 x 140 pixels. The CSS properties just control the displayed size of the element inside the browser, meaning the 140 x 140 are simply stretched to 500 x 500.
So if you get actual pixel data of the canvas - for exporting to gif/png - the final image will have the original dimensions of the canvas - not the rendered.
The fix is quite simple though. Instead of directly using the source canvas for exporting, draw it's content on a second canvas, the size of your desired resolution.
To force the 'export' canvas to not use any filtering/smoothing, you need to set the imageSmoothingEnabled property of it's context to false.
Here's an example (you can right-click and save both images to see the difference):
var ctx = document.getElementById('canvas').getContext('2d');
var image = new Image();
image.onload = function() {
ctx.drawImage(image, 0, 0);
let sourceCanvas = document.getElementById("canvas");
let virtualWidth = parseInt(getComputedStyle(sourceCanvas).width);
let virtualHeight = parseInt(getComputedStyle(sourceCanvas).height);
var canvas = document.getElementById("exportCanvas");
canvas.width = virtualWidth;
canvas.height = virtualHeight;
canvas.getContext('2d').imageSmoothingEnabled = false;
canvas.getContext('2d').drawImage(sourceCanvas, 0, 0, virtualWidth, virtualHeight);
}
image.src = 'https://mdn.mozillademos.org/files/12640/cat.png';
canvas {
width: 500px;
height: 500px;
}
<span>Rendered canvas</span><br>
<canvas id="canvas" width="140" height="140"></canvas><br>
<span>Export canvas</span><br>
<canvas id="exportCanvas"></canvas>

Fill area with fixed size elements

I'm creating an application to calculate how much solar panels would fit on a specific roof.
Users can input the dimensions of their roof.
We only have on size of solar panels available.
I thought a canvas was the way to go but I don't seem to find the information I need..
Requirements
1) Based on the input of the user the canvas should be resized (currently I have a rectangle inside the canvas changing to this size)
2) User should be able to create (and size) objects to put on the roof (chimney, window,..)
3) Based on the open space left solar panels (rectangles) should be automaticly drawn on the canvas
Dimensions and limitations
1px = 2cm
Spacing to edge of roof and object is 7px (14cm)
Solar panel is 169 cm height and 102 cm width
I've checked out the fabric.js library but can't seem to find something close to what I need.
The js I got so far to draw the canvas:
var canvas=document.getElementById("c");
var ctx=canvas.getContext("2d");
var width=50;
var height=35;
var $width=document.getElementById('width');
var $height=document.getElementById('height');
var paneelWidth=101;
var peneelHeight=170;
$width.value=width;
$height.value=height;
draw();
$width.addEventListener("keyup", function(){
width=this.value/2;
draw();
}, false);
$height.addEventListener("keyup", function(){
height=this.value/2;
draw();
}, false);
function draw(){
ctx.clearRect(0,0,canvas.width,canvas.height);
ctx.fillRect(10,10,width,height)
}
Update
The canvas now does resize in a dynamic way based on user input.
I also found the function createPattern(), which is bringing me closer to the solution.
I've added this code to generate a pattern of solar panels in the canvas:
function placepanels(direction) {
ctx.clearRect(0, 0, canvas.width, canvas.height);
var img = document.getElementById("paneel");
var pat = ctx.createPattern(img, direction);
var w2 = canvas.width - 7;
var h2 = canvas.height - 7;
ctx.rect(7, 7, w2, h2);
ctx.fillStyle = pat;
ctx.fill();
}
The -7 on width and height is beacause I need 14cm space on each size of the canvas. Hence why I offset the rectangle containing the pattern 7px from left and top. Currently not able to achieve this on right and bottom side.
Current issue
The result I'm getting is not looking correct, it seems like the pattern repeats wrong (to much repeats) or it's not getting the proper size of the image to repeat.
Updated fiddle: https://jsfiddle.net/8e05ghqy/3/
As for the canvas resize, this function would do it:
changeCanvasSize = function( width, height ) {
$('canvas').width(width)
$('canvas').height(height)
}
Example of usage: changeCanvasSize(450,250) would change the canvas size to 450px of width and 250px of height.
I am just resizing the HTML <canvas> element .width( value ) and .height( value ) works for any HTML element.

infinite background scrolling in javascript html5 canvas [duplicate]

I am looking for a way to wrap a bitmap image around the canvas, for an infinite scrolling effect. I'm looking at EaselJS but clean javascript code will also suffice.
Right now I am displacing an image to the left, and when it reaches a certain mark, it resets itself.
Coming from actionscript, there was an option to "wrap" the pixels of a bitmap around to the other side, thereby never really displacing the image, instead you were wrapping the pixels inside the image. Is this possible in javascript with canvas?
My current code:
this.update = function() {
// super large graphic
_roadContainer.x -= 9;
if(_roadContainer.x < -291) _roadContainer.x = 0;
}
Start with a good landscape image.
Flip the image horizontally using context.scale(-1,1).
Combine the flipped image to the right side of the original image.
Because we have exactly mirrored the images, the far left and right sides of the combined image are exactly the same.
Therefore, as we pan across the combined image and “run out of image”, we can just add another copy of the combined image to the right side and we have infinite + seamless panning.
Here's code and a Fiddle: http://jsfiddle.net/m1erickson/ywDp5/
<!doctype html>
<html>
<head>
<link rel="stylesheet" type="text/css" media="all" href="css/reset.css" /> <!-- reset css -->
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<style>
body{ background-color: ivory; }
canvas{border:1px solid red;}
</style>
<script>
$(function(){
// thanks Paul Irish for this RAF fallback shim
window.requestAnimFrame = (function(callback) {
return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame ||
function(callback) {
window.setTimeout(callback, 1000 / 60);
};
})();
var canvas=document.getElementById("canvas");
var ctx=canvas.getContext("2d");
var infiniteImage;
var infiniteImageWidth;
var img=document.createElement("img");
img.onload=function(){
// use a tempCanvas to create a horizontal mirror image
// This makes the panning appear seamless when
// transitioning to a new image on the right
var tempCanvas=document.createElement("canvas");
var tempCtx=tempCanvas.getContext("2d");
tempCanvas.width=img.width*2;
tempCanvas.height=img.height;
tempCtx.drawImage(img,0,0);
tempCtx.save();
tempCtx.translate(tempCanvas.width,0);
tempCtx.scale(-1,1);
tempCtx.drawImage(img,0,0);
tempCtx.restore();
infiniteImageWidth=img.width*2;
infiniteImage=document.createElement("img");
infiniteImage.onload=function(){
pan();
}
infiniteImage.src=tempCanvas.toDataURL();
}
img.crossOrigin="anonymous";
img.src="https://dl.dropboxusercontent.com/u/139992952/stackoverflow/mountain.jpg";
var fps = 60;
var offsetLeft=0;
function pan() {
// increase the left offset
offsetLeft+=1;
if(offsetLeft>infiniteImageWidth){ offsetLeft=0; }
ctx.drawImage(infiniteImage,-offsetLeft,0);
ctx.drawImage(infiniteImage,infiniteImage.width-offsetLeft,0);
setTimeout(function() {
requestAnimFrame(pan);
}, 1000 / fps);
}
}); // end $(function(){});
</script>
</head>
<body>
<canvas id="canvas" width=500 height=143></canvas><br>
</body>
</html>
You can achieve this quite easily with the html5 canvas.
Look at the drawImage specification here :
http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#drawing-images
drawImage comes in 3 flavors, the first being a simple copy of the image, the second allowing to scale the image, and the third is the one you seek, it allows to perform clipping in a single call.
What you have to do :
- have a counter that will move from zero to the width of your image, then loop to zero again.
- each frame, draw the maximum of the image that you can on the canvas.
- If there is still some part of the canvas not drawn, draw again the image starting from zero to fill the canvas.
i made a fiddle, the only part that matters is in the animate function (other things are tools i often use in my fiddles).
(Rq : I assumed in this example that two images would be enough to fill the canvas.)
http://jsfiddle.net/gamealchemist/5VJhp/
var startx = Math.round(startPos);
var clippedWidth = Math.min(landscape.width - startx, canvasWidth);
// fill left part of canvas with (clipped) image.
ctx.drawImage(landscape, startx, 0, clippedWidth, landscape.height,
0, 0, clippedWidth, landscape.height);
if (clippedWidth < canvasWidth) {
// if we do not fill the canvas
var remaining = canvasWidth - clippedWidth;
ctx.drawImage(landscape, 0, 0, remaining, landscape.height,
clippedWidth, 0, remaining, landscape.height);
}
// have the start position move and loop
startPos += dt * rotSpeed;
startPos %= landscape.width;
To answer my own question: I found a way to achieve this effect with EaselJS. The great benefit of this method is that you don't have to check for the position of your bitmap. It will scroll infinitely - without ever resetting the position to 0.
The trick is to fill a shape with a bitmapfill. You can set a bitmapfill to repeat infinitely. Then you use a Matrix2D to decide the offset of the bitmapfill. Since it repeats automatically, this will generate a scrolling effect.
function.createRoad() {
// road has a matrix, shape and image
_m = new createjs.Matrix2D();
// this gets the image from the preloader - but this can be any image
_r = queue.getResult("road");
// this creates a shape that will hold the repeating bitmap
_roadshape = new createjs.Shape();
// put the shape on the canvas
addChild(_roadshape);
}
//
// the draw code gets repeatedly called, for example by requestanimationframe
//
function.drawRoad() {
// var _speed = 4;
_m.translate(-_speed, 0);
_roadshape.graphics.clear().beginBitmapFill(_r, "repeat", _m).rect(0, 0, 900, 400);
}

javascript: Highlighting image areas / sections

I want to create a simple jsp / servlet code which should have the following:
1) Display an image having different sections. For example: a country map.
2) Allow the user to mark sections on this image using mouse drag. As the user keeps dragging the mouse, the area gets overlay-ed with some different color.
3) As the user moves the mouse, the x and y coordinates on the image should also get calculated. [This feature is however optional.]
The purpose of this application is to mark different 'zones' in an image, which will get saved in the database along with their x-y coordinates.
Can someone please suggest how to achieve this ? Is there any library / API available which could be helpful ?
Regards,
Here is a snippet to get you going. Replace the text setting of the div#server with code to send coordinates to your server. I'll leave the background image for canvas and other important stuff up to you.
var c = $("#map");
var ctx = c[0].getContext("2d");
var down = [0, 0];
var bound = c[0].getBoundingClientRect();
c.mousedown(function(e) {
down = [e.clientX - bound.left, e.clientY - bound.top];
});
c.mouseup(function(e) {
var rect = [
down[0],
down[1], ((e.clientX - bound.left) - down[0]), ((e.clientY - bound.top) - down[1])
];
ctx.fillStyle = "#bbbbbb";
ctx.fillRect(rect[0], rect[1], rect[2], rect[3]);
$("#server").text("Send: " + rect);
});
body {
background-color: lightblue;
}
#map {
background-color: white;
cursor: crosshair;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<canvas id="map" width="256" height="256"></canvas>
<div id="server"></div>

Categories