Imageflip on imagemap, but keep the map coordinates - javascript

I have a rather complex imagemap with several mapped areas.
Most of the mapped areas are just mapped for mouseovers and links. But some arease have a JS onClick() which results in an imageflip. The new, flipped image is basically the old image, with a few "new things" on it.
Two things need to happen which I cannot figure out:
The new image needs to stay there. What is happening no is that as soon as I move the mouse out of the mapped area, it flipps back to the old image.
I need the imagemap (of the "old" image) to work on the new image, which appears on the flip.
JAVASCRIPT
//PRELOAD IMAGES FOR CLICK AND MOUSEOVER
cuentaBoca = new Image(655, 338)
cuentaBoca.src = "imagenes_png/pag2/cuentame_bocadilla.png";
cuenta = new Image(655, 338)
cuenta.src = "imagenes_png/pag2/cuentame_h.png";
//JS FUNCTION FOR CLICK
function bocadillaC() {
document.getElementById('garfio').src = cuentaBoca.src;
}
//JS FUNCTION FOR MOUSEOVER
function cuentaH() {
document.getElementById('garfio').src = cuenta.src;
return true;
}
HTML
<!-- INSERT THE PICTURE -->
<img name="garfio" id="garfio" src="imagenes_png/pag2/base.png" width="655" height="338" border="0" usemap="#m_garfio" alt="" />
<!-- CREATE THE MAP -->
<map name="m_garfio" id="m_garfio">
<area shape="poly" id="bocadilla" coords="7,205,12,197,20,191,24,189,34,185,45,182,58,180,74,180,86,180,94,181,103,182,112,185,114,186,130,178,135,177,137,179,134,184,130,192,135,195,138,199,142,204,143,209,138,218,125,227,113,231,100,235,86,236,70,236,53,235,41,233,34,231,23,226,15,221,11,217,8,212,7,205"
onMouseOver="cuentaH()" onClick="bocadillaC(); return false" alt="" />
</map>
</div>
Here a Fiddel http://jsfiddle.net/emedrs9n/
Clicking on the "Cuentame" (balloon) has the onClick() effect

Try to redefine the original function on click. Example
function bocadillaC() {
cuentaH = original = bocadillaC;
// redefine other functions
leftH = function() {
document.getElementById('garfio').src = left_with_yellow_cloud.src;
};
rightH = function() {
document.getElementById('garfio').src = right_with_yellow_cloud.src;
};
cuentaH = function() {
document.getElementById('garfio').src = cuenta_with_yellow_cloud.src;
};
// ...
document.getElementById('garfio').src = cuentaBoca.src;
}
And you need to redefine other functions with images. But, for example, for this state there is no a similar image with yellow cloud.
But in fact you need to use power of HTML/CSS, don't draw image on each case and do it simpler without image map.

This does what you seem to ask. Two images are displayed. When I roll the mouse over the upper image, the lower image changes and does not change back. The images vane2.jpg and vane2.png are mirror images.
<html>
<head>
<script type="text/javascript">
function setimage () {
document.getElementById('hisArea').src = "vane2.png";
}
</script>
</head>
<body>
<img id="myArea" onMouseOver="setimage();" width="176" height="176" src="vane1.jpg">
<BR>
<img id="hisArea" width="176" height="176" src="vane2.jpg">
</body>
</html>

Related

Sketch.js : Change default brush size

I am using sketch.js library to draw something on canvas. Basically what I need is that the default brush size seems big to me. I want to change the default brush size. Please help in this. I have looked in this link http://intridea.github.io/sketch.js/ but description on this link works after clicking on anchor tab. I want to change the size by default. Any help is appreciated.
somewhere in the sketch.js file there is this piece of code where you can change the default size.
Sketch = (function() {
function Sketch(el, opts) {
this.el = el;
this.canvas = $(el);
this.context = el.getContext('2d');
this.options = $.extend({
toolLinks: true,
defaultTool: 'marker',
defaultColor: '#000000',
defaultSize: 5 //<<< here you can change the default size
}, opts);
If you want to change brush size at user hand then you can use below code:
Sizes :
<img src="img/pencil_icon.png" alt="Pencil"/>
<img src="img/pen_icon.png" alt="Pen"/>
<img src="img/stick_icon.png" alt="Stick"/>
<img src="img/smallbrush_icon.png" alt="Small brush"/>
<img src="img/mediumbrush_icon.png" alt="Medium brush"/>
<img src="img/bigbrush_icon.png" alt="Big brush"/>
<img src="img/bucket_icon.png" alt="Huge bucket"/>
Where canvas is id of your canvas

Know offset of a div placed after lazy load images

I have a div wrapper in which lazy load images are present. Then I have a div below those images and I want to make it Sticky.
<div id="someWrapper">
<img class="lazy" data-original="someImageURL"></img>
<img class="lazy" data-original="someImageURL"></img>
<img class="lazy" data-original="someImageURL"></img>
<img class="lazy" data-original="someImageURL"></img>
<div class="sticky">SomeContents</div> <!-- want to make this sticky on scrool -->
</div>
In order to make them sticky I need offset of the div. Problem is offset is not fixed on the page because of lazy load images that keep pushing the div downward. Image heights are unknown. No of images are 4. Tried using appear event on the last load element but its not giving me accurate results. Please help me how to solve this problem. I want to get offset of the sticky div so I can make a check on the scroll event.
After playing around and some research achieved the desired like this:
function activateStickyScrollEvent(offSetValue){
//code to activate scroll event
}
var lazyLength=$('#someWrapper lazy').length;
var lazyCount=0;
$('#someWrapper lazy').one('appear',function(){
++lazyCount;
if(lazyCount===lazyLength){
var getWrapperOffset=$('#someWrapper').offSet().top;
activateStickyScrollEvent(getWrapperOffset);
})
So, as I said, you may have to check if the last image in the set has been loaded, and then check for the element's offset. Here is a demo of how it could be done. Feel free to adapt the code to suit your needs.
//Ref to the wrapper
var $wrapper = $("#someWrapper");
//Ref to the last image in the set
var lastImgSrc = $wrapper.find(" > img:last").attr("data-original");
var image = new Image();
image.onload = function () {
//do something on load...
var offset = $wrapper.find(".sticky").offset();
alert (offset.top);
}
image.onerror = function () {
//do something on error...
}
image.src = lastImgSrc;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="someWrapper">
<img class="lazy" data-original="someImageURL" />
<img class="lazy" data-original="someImageURL" />
<img class="lazy" data-original="http://dreamatico.com/data_images/sea/sea-4.jpg" src="http://dreamatico.com/data_images/sea/sea-4.jpg" width="100%" />
<img class="lazy" data-original="http://dreamatico.com/data_images/sea/sea-3.jpg" src="http://dreamatico.com/data_images/sea/sea-3.jpg" width="100%" />
<div class="sticky">SomeContents</div> <!-- want to make this sticky on scrool -->
</div>
Hope that helps.
You can count images to load, and the get the offset (OFFSET in my example) :
$(function() {
function imageLoaded() {
counter--;
if( counter === 0 ) {
// Here All your "lazy" images are loaded
var OFFSET = $('#someWrapper').offset().top; <---- you can get offset
}
}
var images = $('.lazy'),
counter = images.length; // initialize the counter
images.each(function() {
if( this.complete ) {
imageLoaded.call( this );
} else {
$(this).one('load', imageLoaded);
}
});
});

Tagging Status in the Image

I have a phonejs project, created for web, using html and js. I need to have a page containing a project construction plan/highrsie building plan image. There are some numbers of lots in that image, lot 1-20 for example, that will having status AVAILABLE, SOLD, BOOKED etc. I want to make the user can tag in the image the status for the corresponding lot, maybe differentiate by colour, for example RED=SOLD, GREEN=AVAILABLE, etc, and the status can be save into database. Could anyone suggest me on what is the best way/method for accomplishing this? Thanks a lot in advance.
I have already tried by using canvas. The following is my code.
HTML
<div data-options="dxView : { name: 'status_tagging', title: 'status_tagging' } " >
<div data-options="dxContent : { targetPlaceholder: 'content' } " >
<h4>Drag the status to the corresponding lot.</h4>
<img id="available" width=32 height=32 src="images/available.png">
<img id="booked" width=32 height=32 src="images/booked.png">
<img id="hold" width=32 height=32 src="images/hold.png">
<img id="reserved" width=32 height=32 src="images/reserved.png">
<img id="sold" width=32 height=32 src="images/sold.png">
<img id="siteplan" src="images/siteplan.jpg">
<br>
<canvas id="myCanvas" width=300 height=300 style="border:1px solid #d3d3d3;"></canvas>
</div>
JavaScript
KioskAdminV2.status_tagging = function (params) {
var id = params.id;
var tb_prop_siteplan = new KioskAdminV2.tb_prop_siteplanViewModel();
var canvas=document.getElementById("myCanvas");
var ctx=canvas.getContext("2d");
var $canvas=$("#myCanvas");
var canvasOffset=$canvas.offset();
var offsetX=canvasOffset.left;
var offsetY=canvasOffset.top;
var img=document.getElementById("siteplan");
ctx.drawImage(img,10,10);
var image1=new Image();
image1.src = "images/available.png";
var image2=new Image();
image2.src = "images/booked.png";
var $available=$("#available");
var $booked=$("#booked");
var $canvas=$("#myCanvas");
$available.draggable({
helper:'clone'
});
$booked.draggable({
helper:'clone'
});
// set the data payload
$available.data("image",image1); // key-value pair
$booked.data("image",image2);
$canvas.droppable({
drop:dragDrop
});
function dragDrop(e,ui){
var element=ui.draggable;
var data=element.data("url");
var x=parseInt(ui.offset.left-offsetX);
var y=parseInt(ui.offset.top-offsetY);
ctx.drawImage(element.data("image"),x-1,y);
}
return {
id:id,
tb_prop_siteplan: tb_prop_siteplan,
canvas:canvas,
ctx:ctx,
$canvas:$canvas,
canvasOffset:canvasOffset,
offsetX:offsetX,
offsetY:offsetY,
img:img,
image1:image1,
image2:image2,
$available:$available,
$booked: $booked,
dragDrop: dragDrop
};
};
But i got error "cannot read property of 'getContext' of null". Can anyone tell me why is this?
You could do your project in Canvas or SVG, but...
Since your building plan is fixed in shape, you don't need them to do your project.
Just use html+css -- simpler to learn + more html+css tutorials online.
Cut your building plan into lots and make an image of each lot.
Make both a green and red shaded version of each lot.
Listen for mouseclick events and determine which lot the use clicked in.
Toggle the image for that lot from red-to-yellow-to-green representing sold-booked-available.

How to load image on demand

I have this simple image zoom jQuery. Here is a Demo example. It uses the elevateZoom jQuery.
The code behind is very simple:
<img id="zoom_05" src='small_image1.png' data-zoom-image="large_image1.jpg"/>
<script>
$vc("#zoom_05").elevateZoom({
zoomType : "inner",
cursor: "crosshair"
});
</script>
My question, is how can i make the large image load on demand, only when the mouse is over it. Please have a look at this demo example and let me know what i need to add in the code.
img element (id="zoom_05") above, would not load large_image1.jpg on its own.
Large image load happens because elevateZoom() looks into its data-zoom-image value and immediately loads it. One way around this behaviour is to defer elevateZoom() until user hover's over the small image for the first time. Quick example:
jQuery( function () {
var elevate_zoom_attached = false, $zoom_05 = $("#zoom_05") ;
$zoom_05.hover(
// hover IN
function () {
if ( ! elevate_zoom_attached ) {
$zoom_05.elevateZoom({
zoomType : "inner",
cursor : "crosshair"
});
elevate_zoom_attached = true ;
};
},
// hover OUT
function () {
if ( elevate_zoom_attached) { // no need for hover any more
$zoom_05.off("hover");
}
}
);
}) ;
Mind you this is an quick, on-top-of-my-head code, but should work ...
Also in this case elevateZoom() action might not be immediate while large image loading is going on.
I used this idea to initiate zoom on any number of images on the same page by adding a zoom class to the image. It works without any HOVER OUT
<img class="zoom" src="myimage.png" data-zoom-image="mybigimage.png"/>
$('.zoom').hover(
// hover IN
function () {
var currImg = $(this); //get the current image
if (!currImg.hasClass('zoomon')) { //if it hasn't been initialized
currImg.elevateZoom(); //initialize elevateZoom
currImg.addClass('zoomon'); //add the zoomon class to the img so it doesn't re-initialize
}
})

Save canvas to image via toDataURL failed

I create a test code below and you can manipulate it on Jsfiddle:
http://jsfiddle.net/Stallman41/57hvX/31/
HTML:
<canvas id="test_canvas" style="background-color : #FFFF00" ; width="500px"
; height="340px"></canvas>
<br>
<button id="test_put_btn">Put an image</button>
<br>
<button id="save_dataURL">Save to dataURL</button>
<br>
<button id="draw_back">Final step: draw 3 images back.</button>
<br>
<img id="first_img"; width="100px" ; height="100px" ;></img>
<img id="second_img"; width="100px" ; height="100px" ></img>
<img id="third_img"; width="100px" ; height="100px" ;></img>
Javascript:
var drawing_plate;
var context;
var dataURL_arr = new Array();
$(document).ready(function () {
drawing_plate = document.getElementById("test_canvas");
context = drawing_plate.getContext('2d');
$("#test_canvas").bind("mousedown", Touch_Start);
$("#test_canvas").bind("mousemove", Touch_Move);
$("#test_canvas").bind("mouseup", Touch_End);
}); //document ready.
function Touch_Start(event) {
event.preventDefault();
touch = event;
touch_x = touch.pageX;
touch_y = touch.pageY;
line_start_x = touch.pageX - 0;
line_start_y = touch.pageY - 0;
context.beginPath();
context.moveTo(line_start_x, line_start_y);
}
function Touch_Move(event) {
event.preventDefault();
touch = event; //mouse
line_end_x = touch.pageX - 0;
line_end_y = touch.pageY - 0;
context.lineTo(line_end_x, line_end_y);
context.stroke();
}
$("#test_put_btn").click(function () {
var test_img = new Image();
test_img.src = "http://careerscdn.sstatic.net/careers/gethired/img/careers2- ad-header-so-crop.png";
context.drawImage(test_img, 0, 0);
});
$("#save_dataURL").click(function () {
dataURL_arr.push(drawing_plate.toDataURL("image/png"));
});
$("#draw_back").click(function () {
var f_image= $("#first_img")[0];
var s_image= $("#second_img")[0];
var t_image= $("#third_img")[0];
f_image.onload= function()
{
f_image.src= dataURL_arr[0];
}
f_image.src= dataURL_arr[0];
s_image.onload= function()
{
s_image.src= dataURL_arr[0];
}
s_image.src= dataURL_arr[0];
t_image.onload= function()
{
t_image.src= dataURL_arr[0];
}
t_image.src= dataURL_arr[0];
});
I develop a drawing plate on Android system, saving the drawings to a dataURL string. They can draw something on the canvas and put images on the canvas. And I need to let the users see their drawings on small icons.
I use canvas.toDataURL("image/png") to save the base64 string. And I choose <img> as the small icon container. However, what I got is only the drawings can be shown on the icon, and usually, when I write img.src= canvas.toDataURL("image/png"); the image shows nothing!
I investigate the issue for long time.
1. I think the problem might be the dataURL string is too long?
2. The support of the OS: Android?
The code in Jsfiddle here shows a similar procedure on my Android PhoneGap development.
First , you just draw something on the canvas, and press Press an image, and then Save to dataURL. But you should do the process three times. In this condition, the string array contains the base64 string generated by the drawings and the image.
In the final, you press Final step: draw 3 images back., nothing will be shown on the image icon.
In conclusion:
In my experience, as I write img.src= canvas.toDataURL("image/png"); (no matter the img is an dom element or var img = new Image();). It can't always work: sometimes it works... but sometimes not...(I work on Android 4.0.1, phonegap 1.7.0)
Second, especially if I store lots of base64 strings to an array, assigning them to lots of image DOM element, it definitely fails.
Third, if the user only draw something on the canvas, it can always work.( Except the example code in the Jsfiddle, but it works on my Android system...)
But if he draw an image context.drawImage(~) the image wouldn't show the pic.
Too much confusions...
I need to let the user can view their drawings in small icon, any alternative?
Some References:
1
2
3
I just stumbled across this question.
Click Put an image, then click Save to dataURL, then check your JavaScript console for something like:
SecurityError: DOM Exception 18
It's a browser security feature. Because you've inserted an image from a different domain, it counts as a cross-origin request.
If you eliminate the security error, you can export the canvas to a data URL.
Another thing in your code.
The image you try to draw onto the canvas into your test_put_btn onclick event handler, your image will never show up (or it will sometimes work accidentally) because you don't wait for your image to be loaded to draw it onto the canvas.
You have to handle the "onload" event of your image and draw it into the handler to permit the drawing of your image.
Before your test_img.src statement, you have to put :
test_img.onload = function()
{
context.drawImage(test_img, 0, 0);
};
Plus, the image you try to access is not accessible --> For me it does not work

Categories

Resources