I've seen some cool image masking and other effects with CSS3. Using only CSS3 can we achieve this effect seen in the code below or see working fiddle.
http://jsfiddle.net/s6u9a/
HTML
<canvas id="canvas1" width="400" height="400"></canvas>
Javascript
var can = document.getElementById('canvas1');
var ctx = can.getContext('2d');
can.addEventListener('mousemove', function(e) {
var mouse = getMouse(e, can);
redraw(mouse);
}, false);
function redraw(mouse) {
console.log('a');
can.width = can.width;
ctx.drawImage(img, 0, 0);
ctx.beginPath();
ctx.rect(0,0,500,500);
ctx.arc(mouse.x, mouse.y, 70, 0, Math.PI*2, true)
ctx.clip();
ctx.drawImage(img2, 0, 0);
}
var img = new Image();
img.onload = function() {
redraw({x: -450, y: -500})
}
img.src = 'http://placekitten.com/400/400';
var img2 = new Image();
img2.onload = function() {
redraw({x: -450, y: -500})
}
img2.src = 'http://placekitten.com/400/395';
function getMouse(e, canvas) {
var element = canvas,
offsetX = 0,
offsetY = 0,
mx, my;
if (element.offsetParent !== undefined) {
do {
offsetX += element.offsetLeft;
offsetY += element.offsetTop;
} while ((element = element.offsetParent));
}
mx = e.pageX - offsetX;
my = e.pageY - offsetY;
return {
x: mx,
y: my
};
}
Here's an alternate approach using background-image. It's more flexible, and you get a circular (or arbitrarily shaped) viewport:
http://jsfiddle.net/maackle/66uCs/
HTML
<div class="masker">
<img class="base" src="http://lorempixel.com/400/400/cats/1" />
<div class="overlay"></div>
</div>
CSS
.masker {
position: relative;
}
.overlay {
position: absolute;
display: none;
width: 100px;
height: 100px;
border-radius: 50%;
background: url(http://lorempixel.com/400/400/cats/2) no-repeat;
}
jQuery 1.10.1
$('.masker').on('mousemove', function (e) {
var r, x, y, top, left, bottom, right, attr, $base, $overlay;
r = 100;
$base = $(this).find('.base');
$overlay = $(this).find('.overlay');
x = e.pageX - $base.offset().left;
y = e.pageY - $base.offset().top;
top = y - r / 2;
left = x - r / 2;
if (x < 0 || y < 0 || x > $base.width() || y > $base.height()) {
$overlay.hide();
} else {
$overlay.show().css({
'background-position': '' + (-left) + 'px ' + (-top) + 'px',
'left': left,
'top': top
});
}
});
//just for good measure
$('.masker').on('mouseout', function (e) {
$(this).find('.overlay').hide();
});
Here's a solution that uses mostly CSS -- you do need some Javascript to detect the mouse position. Note that this gives you a square viewport instead of a circle, but if CSS ever adds circle() alongside rect() for the clip property, you will have that option:
http://jsfiddle.net/maackle/Yc2b4/
HTML
<div class="masker">
<img class="base" src="http://lorempixel.com/400/400/cats/1" />
<img class="overlay" src="http://lorempixel.com/400/400/cats/2" />
</div>
CSS
.masker {
position: relative;
}
.overlay {
position: absolute;
top: 0;
left: 0;
display: none;
}
jQuery 1.10.1
$('.masker img').on('mousemove', function(e) {
var r, x, y, top, left, bottom, right, attr, $overlay;
r = 100;
x = e.pageX - $(this).offset().left;
y = e.pageY - $(this).offset().top;
top = y - r/2;
left = x - r/2;
bottom = y + r/2;
right = x + r/2;
attr = 'rect('+(top)+'px, '+(right)+'px, '+(bottom)+'px, '+(left)+'px)';
$overlay = $('.masker .overlay');
$overlay.show().css({clip: attr});
});
$('.masker img').on('mouseout', function(e) {
$('.masker .overlay').hide();
});
Related
I'm trying to use this interaction but on 3 canvas in a row. I've been trying to play around with it for some time and failed to add multiple canvas that use the same interaction without interfering with one another.
I thought of uniquely call them canvas1, canvas2 and canvas3, but I'm wondering if anyone knew a simpler way?
I made a rough hide/show jquery version of what I have in mind but instead, it would use that scratching interaction.
Thank you!
$(".image1").click(function(){
$(".image1").hide();
$("p").show();
});
$(".image2").click(function(){
$(".image2").hide();
$("p").show();
});
$(".image3").click(function(){
$(".image3").hide();
$("p").show();
});
.pannel{
width: calc(33% - 40px);
height: 220px;
float: left;
margin: 0 20px;
}
.image1, .image2, .image3{
max-width:100px !important;
background-size: contain;
background-repeat: no-repeat;
background-position: bottom;
position:absolute;
}
.text-div{
width: 80%;
text-align: center;
margin:auto;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<div class="container">
<div class="row pannels">
<div class="col-lg-12">
<div class="pannels-div">
<div class="pannel ">
<div class="image1"><img src="https://www.patrickmorin.com/media/catalog/product/cache/e4d64343b1bc593f1c5348fe05efa4a6/a/s/asphalt-shingle-lifetime-1-BAREVGL-fr.jpg" width="220"></div>
<div class="text-div">
<p> Vestibulum tempor erat at mauris aliquet, eu laoreet mi imperdiet.</p>
</div>
</div>
<div class="pannel">
<div class="image2"><img src="https://www.patrickmorin.com/media/catalog/product/cache/e4d64343b1bc593f1c5348fe05efa4a6/a/s/asphalt-shingle-lifetime-1-BAREVGL-fr.jpg" width="220"></div>
<div class="text-div">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec dignissim risus et orci fringilla semper.</p>
</div>
</div>
<div class="pannel">
<div class="image3"><img src="https://www.patrickmorin.com/media/catalog/product/cache/e4d64343b1bc593f1c5348fe05efa4a6/a/s/asphalt-shingle-lifetime-1-BAREVGL-fr.jpg" width="220"></div>
<div class="text-div">
<p>Praesent eget congue sem, tempor fermentum augue. Duis vulputate, libero vitae efficitur convallis.</p>
</div>
</div>
</div>
</div>
</div>
</div>
Okay so I've worked it out by adding a number on each class and each canvas's. It would be nice to know if there's an optimal way to use the same drawing effect but for multiple canvas.
The code is a mess, but I guess it works now
(function () {
'use strict';
/* 1er */
var isDrawing1, lastPoint1;
var container1 = document.getElementById('js-container1');
var canvas1 = document.getElementById('js-canvas1');
var canvas1Width = canvas1.width;
var canvas1Height = canvas1.height;
var ctx1 = canvas1.getContext('2d');
var image1 = new Image();
var brush1 = new Image();
/* 2e */
var isDrawing2, lastPoint2;
var container2 = document.getElementById('js-container2');
var canvas2 = document.getElementById('js-canvas2');
var canvas2Width = canvas2.width;
var canvas2Height = canvas2.height;
var ctx2 = canvas2.getContext('2d');
var image2 = new Image();
var brush2 = new Image();
/* 3e */
var isDrawing3, lastPoint3;
var container3 = document.getElementById('js-container3');
var canvas3 = document.getElementById('js-canvas3');
var canvas3Width = canvas3.width;
var canvas3Height = canvas3.height;
var ctx3 = canvas3.getContext('2d');
var image3 = new Image();
var brush3 = new Image();
/* Load top images */
image1.src = 'http://placehold.it/324x289/145f46?text=Canva1';
image2.src = 'http://placehold.it/324x289/145f46?text=Canva2';
image3.src = 'http://placehold.it/324x289/145f46?text=Canva3';
draw1();
draw2();
draw3();
function draw1() {
// image du dessus
image1.onload = function () {
ctx1.drawImage(image1, 0, 0);
// Show the scratchText when Image is loaded.
document.querySelectorAll('.scratchText1')[0].style.visibility = 'visible';
};
//image du brush a scratch
brush1.src = 'https://icon-library.com/images/3-dots-icon/3-dots-icon-9.jpg';
//Mouvement de la souris
canvas1.addEventListener('mousedown', handleMouseDown, false);
canvas1.addEventListener('touchstart', handleMouseDown, false);
canvas1.addEventListener('mousemove', handleMouseMove, false);
canvas1.addEventListener('touchmove', handleMouseMove, false);
canvas1.addEventListener('mouseup', handleMouseUp, false);
canvas1.addEventListener('touchend', handleMouseUp, false);
function distanceBetween(point1, point2) {
return Math.sqrt(Math.pow(point2.x - point1.x, 2) + Math.pow(point2.y - point1.y, 2));
}
function angleBetween(point1, point2) {
return Math.atan2(point2.x - point1.x, point2.y - point1.y);
}
function getMouse(e, canvas1) {
var offsetX = 0,
offsetY = 0,
mx, my;
if (canvas1.offsetParent !== undefined) {
do {
offsetX += canvas1.offsetLeft;
offsetY += canvas1.offsetTop;
} while ((canvas1 = canvas1.offsetParent));
}
mx = (e.pageX || e.touches[0].clientX) - offsetX;
my = (e.pageY || e.touches[0].clientY) - offsetY;
return {
x: mx,
y: my
};
}
function handleMouseDown(e) {
isDrawing1 = true;
lastPoint1 = getMouse(e, canvas1);
}
function handleMouseMove(e) {
if (!isDrawing1) {
return;
}
e.preventDefault();
var currentPoint = getMouse(e, canvas1),
dist = distanceBetween(lastPoint1, currentPoint),
angle = angleBetween(lastPoint1, currentPoint),
x, y;
for (var i = 0; i < dist; i++) {
x = lastPoint1.x + (Math.sin(angle) * i) - 25;
y = lastPoint1.y + (Math.cos(angle) * i) - 25;
ctx1.globalCompositeOperation = 'destination-out';
ctx1.drawImage(brush1, x, y);
}
lastPoint1 = currentPoint;
}
function handleMouseUp(e) {
isDrawing1 = false;
}
}
/* 2e */
function draw2() {
// image du dessus
image2.onload = function () {
ctx2.drawImage(image2, 0, 0);
// Show the scratchText when Image is loaded.
document.querySelectorAll('.scratchText2')[0].style.visibility = 'visible';
};
//image du brush a scratch
brush2.src = 'https://icon-library.com/images/3-dots-icon/3-dots-icon-9.jpg';
//Mouvement de la souris
canvas2.addEventListener('mousedown', handleMouseDown, false);
canvas2.addEventListener('touchstart', handleMouseDown, false);
canvas2.addEventListener('mousemove', handleMouseMove, false);
canvas2.addEventListener('touchmove', handleMouseMove, false);
canvas2.addEventListener('mouseup', handleMouseUp, false);
canvas2.addEventListener('touchend', handleMouseUp, false);
function distanceBetween(point1, point2) {
return Math.sqrt(Math.pow(point2.x - point1.x, 2) + Math.pow(point2.y - point1.y, 2));
}
function angleBetween(point1, point2) {
return Math.atan2(point2.x - point1.x, point2.y - point1.y);
}
function getMouse(e, canvas2) {
var offsetX = 0,
offsetY = 0,
mx, my;
if (canvas2.offsetParent !== undefined) {
do {
offsetX += canvas2.offsetLeft;
offsetY += canvas2.offsetTop;
} while ((canvas2 = canvas2.offsetParent));
}
mx = (e.pageX || e.touches[0].clientX) - offsetX;
my = (e.pageY || e.touches[0].clientY) - offsetY;
return {
x: mx,
y: my
};
}
function handleMouseDown(e) {
isDrawing2 = true;
lastPoint2 = getMouse(e, canvas2);
}
function handleMouseMove(e) {
if (!isDrawing2) {
return;
}
e.preventDefault();
var currentPoint = getMouse(e, canvas2),
dist = distanceBetween(lastPoint2, currentPoint),
angle = angleBetween(lastPoint2, currentPoint),
x, y;
for (var i = 0; i < dist; i++) {
x = lastPoint2.x + (Math.sin(angle) * i) - 25;
y = lastPoint2.y + (Math.cos(angle) * i) - 25;
ctx2.globalCompositeOperation = 'destination-out';
ctx2.drawImage(brush2, x, y);
}
lastPoint2 = currentPoint;
}
function handleMouseUp(e) {
isDrawing2 = false;
}
}
/* 3e */
function draw3() {
// image du dessus
image3.onload = function () {
ctx3.drawImage(image3, 0, 0);
// Show the scratchText when Image is loaded.
document.querySelectorAll('.scratchText3')[0].style.visibility = 'visible';
};
//image du brush a scratch
brush3.src = 'https://icon-library.com/images/3-dots-icon/3-dots-icon-9.jpg';
//Mouvement de la souris
canvas3.addEventListener('mousedown', handleMouseDown, false);
canvas3.addEventListener('touchstart', handleMouseDown, false);
canvas3.addEventListener('mousemove', handleMouseMove, false);
canvas3.addEventListener('touchmove', handleMouseMove, false);
canvas3.addEventListener('mouseup', handleMouseUp, false);
canvas3.addEventListener('touchend', handleMouseUp, false);
function distanceBetween(point1, point2) {
return Math.sqrt(Math.pow(point2.x - point1.x, 2) + Math.pow(point2.y - point1.y, 2));
}
function angleBetween(point1, point2) {
return Math.atan2(point2.x - point1.x, point2.y - point1.y);
}
function getMouse(e, canvas3) {
var offsetX = 0,
offsetY = 0,
mx, my;
if (canvas3.offsetParent !== undefined) {
do {
offsetX += canvas3.offsetLeft;
offsetY += canvas3.offsetTop;
} while ((canvas3 = canvas3.offsetParent));
}
mx = (e.pageX || e.touches[0].clientX) - offsetX;
my = (e.pageY || e.touches[0].clientY) - offsetY;
return {
x: mx,
y: my
};
}
function handleMouseDown(e) {
isDrawing3 = true;
lastPoint3 = getMouse(e, canvas3);
}
function handleMouseMove(e) {
if (!isDrawing3) {
return;
}
e.preventDefault();
var currentPoint = getMouse(e, canvas3),
dist = distanceBetween(lastPoint3, currentPoint),
angle = angleBetween(lastPoint3, currentPoint),
x, y;
for (var i = 0; i < dist; i++) {
x = lastPoint3.x + (Math.sin(angle) * i) - 25;
y = lastPoint3.y + (Math.cos(angle) * i) - 25;
ctx3.globalCompositeOperation = 'destination-out';
ctx3.drawImage(brush3, x, y);
}
lastPoint3 = currentPoint;
}
function handleMouseUp(e) {
isDrawing3 = false;
}
}
})();
body {
padding: 20px 0;
}
.container1, .container2, .container3 {
border: 3px solid yellow;
position: relative;
width: 324px;
height: 289px;
margin: 0 auto;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
-o-user-select: none;
user-select: none;
}
.canvas1 {
position: absolute;
top: 0;
}
.canvas2 {
position: absolute;
top: 0;
}
.canvas3 {
position: absolute;
top: 0;
}
.scratchText1 {
padding: 20px;
}
.scratchText2 {
padding: 20px;
}
.scratchText3 {
padding: 20px;
}
<div class="container">
<div class="container1">
<div id="js-container1">
<canvas class="canvas1" id="js-canvas1" width="324" height="289"></canvas>
<div class="scratchText1" style="visibility: hidden;">
<h2>'Allo, 'Allo!</h2>
<h3>The secret code is:</h3>
<h1><code>HlkafSYc</code></h1>
</div>
</div>
</div>
<div class="container2">
<div id="js-container2">
<canvas class="canvas2" id="js-canvas2" width="324" height="289"></canvas>
<div class="scratchText2" style="visibility: hidden;">
<h2>'Allo, 'Allo!</h2>
<h3>The secret code is:</h3>
<h1><code>HlkafSYc</code></h1>
</div>
</div>
</div>
<div class="container3">
<div id="js-container3">
<canvas class="canvas3" id="js-canvas3" width="324" height="289"></canvas>
<div class="scratchText3" style="visibility: hidden;">
<h2>'Allo, 'Allo!</h2>
<h3>The secret code is:</h3>
<h1><code>HlkafSYc</code></h1>
</div>
</div>
</div>
</div>
How to make Zooming effect like these, when hovering the image Sample Image
$(document).ready(function(){
var native_width = 0;
var native_height = 0;
//Now the mousemove function
$(".magnify").mousemove(function(e){
//When the user hovers on the image, the script will first calculate
//the native dimensions if they don't exist. Only after the native dimensions
//are available, the script will show the zoomed version.
if(!native_width && !native_height)
{
//This will create a new image object with the same image as that in .small
//We cannot directly get the dimensions from .small because of the
//width specified to 200px in the html. To get the actual dimensions we have
//created this image object.
var image_object = new Image();
image_object.src = $(".small").attr("src");
//This code is wrapped in the .load function which is important.
//width and height of the object would return 0 if accessed before
//the image gets loaded.
native_width = image_object.width;
native_height = image_object.height;
}
else
{
//x/y coordinates of the mouse
//This is the position of .magnify with respect to the document.
var magnify_offset = $(this).offset();
//We will deduct the positions of .magnify from the mouse positions with
//respect to the document to get the mouse positions with respect to the
//container(.magnify)
var mx = e.pageX - magnify_offset.left;
var my = e.pageY - magnify_offset.top;
//Finally the code to fade out the glass if the mouse is outside the container
if(mx < $(this).width() && my < $(this).height() && mx > 0 && my > 0)
{
$(".large").fadeIn(100);
}
else
{
$(".large").fadeOut(100);
}
if($(".large").is(":visible"))
{
//The background position of .large will be changed according to the position
//of the mouse over the .small image. So we will get the ratio of the pixel
//under the mouse pointer with respect to the image and use that to position the
//large image inside the magnifying glass
var rx = Math.round(mx/$(".small").width()*native_width - $(".large").width()/2)*-1;
var ry = Math.round(my/$(".small").height()*native_height - $(".large").height()/2)*-1;
var bgp = rx + "px " + ry + "px";
//Time to move the magnifying glass with the mouse
var px = mx - $(".large").width()/2;
var py = my - $(".large").height()/2;
//Now the glass moves with the mouse
//The logic is to deduct half of the glass's width and height from the
//mouse coordinates to place it with its center at the mouse coordinates
//If you hover on the image now, you should see the magnifying glass in action
$(".large").css({left: px, top: py, backgroundPosition: bgp});
}
}
})
})
/*Some CSS*/
* {margin: 0; padding: 0;}
.magnify {width: 200px; margin: 50px auto; position: relative;}
/*Lets create the magnifying glass*/
.large {
width: 175px; height: 175px;
position: absolute;
border-radius: 100%;
/*Multiple box shadows to achieve the glass effect*/
box-shadow: 0 0 0 7px rgba(255, 255, 255, 0.85),
0 0 7px 7px rgba(0, 0, 0, 0.25),
inset 0 0 40px 2px rgba(0, 0, 0, 0.25);
/*Lets load up the large image first*/
background: url('http://thecodeplayer.com/uploads/media/iphone.jpg') no-repeat;
/*hide the glass by default*/
display: none;
}
/*To solve overlap bug at the edges during magnification*/
.small { display: block; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- Lets make a simple image magnifier -->
<div class="magnify">
<!-- This is the magnifying glass which will contain the original/large version -->
<div class="large"></div>
<!-- This is the small image -->
<img class="small" src="http://thecodeplayer.com/uploads/media/iphone.jpg" width="200"/>
</div>
<!-- Lets load up prefixfree to handle CSS3 vendor prefixes -->
<script src="http://thecodeplayer.com/uploads/js/prefixfree.js" type="text/javascript"></script>
<!-- You can download it from http://leaverou.github.com/prefixfree/ -->
<!-- Time for jquery action -->
<script src="http://thecodeplayer.com/uploads/js/jquery-1.7.1.min.js" type="text/javascript"></script>
Try this one
(function ($) {
$(document).ready(function() {
$('.xzoom, .xzoom-gallery').xzoom({zoomWidth: 400, title: true, tint: '#333', Xoffset: 15});
$('.xzoom2, .xzoom-gallery2').xzoom({position: '#xzoom2-id', tint: '#ffa200'});
$('.xzoom3, .xzoom-gallery3').xzoom({position: 'lens', lensShape: 'circle', sourceClass: 'xzoom-hidden'});
$('.xzoom4, .xzoom-gallery4').xzoom({tint: '#006699', Xoffset: 15});
$('.xzoom5, .xzoom-gallery5').xzoom({tint: '#006699', Xoffset: 15});
//Integration with hammer.js
var isTouchSupported = 'ontouchstart' in window;
if (isTouchSupported) {
//If touch device
$('.xzoom, .xzoom2, .xzoom3, .xzoom4, .xzoom5').each(function(){
var xzoom = $(this).data('xzoom');
xzoom.eventunbind();
});
$('.xzoom, .xzoom2, .xzoom3').each(function() {
var xzoom = $(this).data('xzoom');
$(this).hammer().on("tap", function(event) {
event.pageX = event.gesture.center.pageX;
event.pageY = event.gesture.center.pageY;
var s = 1, ls;
xzoom.eventmove = function(element) {
element.hammer().on('drag', function(event) {
event.pageX = event.gesture.center.pageX;
event.pageY = event.gesture.center.pageY;
xzoom.movezoom(event);
event.gesture.preventDefault();
});
}
xzoom.eventleave = function(element) {
element.hammer().on('tap', function(event) {
xzoom.closezoom();
});
}
xzoom.openzoom(event);
});
});
$('.xzoom4').each(function() {
var xzoom = $(this).data('xzoom');
$(this).hammer().on("tap", function(event) {
event.pageX = event.gesture.center.pageX;
event.pageY = event.gesture.center.pageY;
var s = 1, ls;
xzoom.eventmove = function(element) {
element.hammer().on('drag', function(event) {
event.pageX = event.gesture.center.pageX;
event.pageY = event.gesture.center.pageY;
xzoom.movezoom(event);
event.gesture.preventDefault();
});
}
var counter = 0;
xzoom.eventclick = function(element) {
element.hammer().on('tap', function() {
counter++;
if (counter == 1) setTimeout(openfancy,300);
event.gesture.preventDefault();
});
}
function openfancy() {
if (counter == 2) {
xzoom.closezoom();
$.fancybox.open(xzoom.gallery().cgallery);
} else {
xzoom.closezoom();
}
counter = 0;
}
xzoom.openzoom(event);
});
});
$('.xzoom5').each(function() {
var xzoom = $(this).data('xzoom');
$(this).hammer().on("tap", function(event) {
event.pageX = event.gesture.center.pageX;
event.pageY = event.gesture.center.pageY;
var s = 1, ls;
xzoom.eventmove = function(element) {
element.hammer().on('drag', function(event) {
event.pageX = event.gesture.center.pageX;
event.pageY = event.gesture.center.pageY;
xzoom.movezoom(event);
event.gesture.preventDefault();
});
}
var counter = 0;
xzoom.eventclick = function(element) {
element.hammer().on('tap', function() {
counter++;
if (counter == 1) setTimeout(openmagnific,300);
event.gesture.preventDefault();
});
}
function openmagnific() {
if (counter == 2) {
xzoom.closezoom();
var gallery = xzoom.gallery().cgallery;
var i, images = new Array();
for (i in gallery) {
images[i] = {src: gallery[i]};
}
$.magnificPopup.open({items: images, type:'image', gallery: {enabled: true}});
} else {
xzoom.closezoom();
}
counter = 0;
}
xzoom.openzoom(event);
});
});
} else {
//If not touch device
//Integration with fancybox plugin
$('#xzoom-fancy').bind('click', function(event) {
var xzoom = $(this).data('xzoom');
xzoom.closezoom();
$.fancybox.open(xzoom.gallery().cgallery, {padding: 0, helpers: {overlay: {locked: false}}});
event.preventDefault();
});
//Integration with magnific popup plugin
$('#xzoom-magnific').bind('click', function(event) {
var xzoom = $(this).data('xzoom');
xzoom.closezoom();
var gallery = xzoom.gallery().cgallery;
var i, images = new Array();
for (i in gallery) {
images[i] = {src: gallery[i]};
}
$.magnificPopup.open({items: images, type:'image', gallery: {enabled: true}});
event.preventDefault();
});
}
});
})(jQuery);
<link href="https://unpkg.com/xzoom#1.0.7/dist/xzoom.css" rel="stylesheet"/>
<script src="https://code.jquery.com/jquery-2.1.1.js"></script>
<script src="https://unpkg.com/xzoom#1.0.7/dist/xzoom.min.js"></script>
<script src="https://hammerjs.github.io/dist/hammer.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/foundation/6.3.1/js/foundation.min.js"></script>
<body>
<div class="container">
<!-- default start -->
<section id="default" class="padding-top0">
<div class="row">
<div class="large-12 column"><h3>Product Zooming</h3></div>
<hr>
<div class="large-5 column">
<div class="xzoom-container">
<img class="xzoom" id="xzoom-default" src="http://www.jqueryscript.net/demo/Feature-rich-Product-Gallery-With-Image-Zoom-xZoom/images/gallery/preview/01_b_car.jpg" xoriginal="http://www.jqueryscript.net/demo/Feature-rich-Product-Gallery-With-Image-Zoom-xZoom/images/gallery/original/01_b_car.jpg" />
<div class="xzoom-thumbs">
<img class="xzoom-gallery" width="80" src="http://www.jqueryscript.net/demo/Feature-rich-Product-Gallery-With-Image-Zoom-xZoom/images/gallery/thumbs/01_b_car.jpg" xpreview="http://www.jqueryscript.net/demo/Feature-rich-Product-Gallery-With-Image-Zoom-xZoom/images/gallery/preview/01_b_car.jpg" title="The description goes here">
<img class="xzoom-gallery" width="80" src="http://www.jqueryscript.net/demo/Feature-rich-Product-Gallery-With-Image-Zoom-xZoom/images/gallery/preview/02_o_car.jpg" title="The description goes here">
<img class="xzoom-gallery" width="80" src="http://www.jqueryscript.net/demo/Feature-rich-Product-Gallery-With-Image-Zoom-xZoom/images/gallery/preview/03_r_car.jpg" title="The description goes here">
<img class="xzoom-gallery" width="80" src="http://www.jqueryscript.net/demo/Feature-rich-Product-Gallery-With-Image-Zoom-xZoom/images/gallery/preview/04_g_car.jpg" title="The description goes here">
</div>
</div>
</div>
<div class="large-7 column"></div>
</div>
</section>
<!-- default end -->
</div>
</body>
On any given application, is there a way, maybe in dev tools where I can check the pixel location of my mouse hover?
Use javascript to get cursor location.
document.addEventListener("mouseover", function( event ) {
console.log(event.screenX, event.screenY);
}, false);
Getting the Mouse Click Position :
<!DOCTYPE html>
<html>
<head>
<title>Move to Click Position</title>
<style type="text/css">
body {
background-color: #FFF;
margin: 30px;
margin-top: 10px;
}
#contentContainer {
width: 550px;
height: 350px;
border: 5px black solid;
overflow: hidden;
background-color: #F2F2F2;
cursor: pointer;
}
#thing {
position: relative;
left: 50px;
top: 50px;
transition: left .5s ease-in, top .5s ease-in;
}
</style>
</head>
<body>
<div id="contentContainer">
<img id="thing" src="//www.kirupa.com/images/smiley_red.png">
</div>
<script src="//www.kirupa.com/prefixfree.min.js"></script>
<script>
var theThing = document.querySelector("#thing");
var container = document.querySelector("#contentContainer");
container.addEventListener("click", getClickPosition, false);
function getClickPosition(e) {
var parentPosition = getPosition(e.currentTarget);
var xPosition = e.clientX - parentPosition.x - (theThing.clientWidth / 2);
var yPosition = e.clientY - parentPosition.y - (theThing.clientHeight / 2);
theThing.style.left = xPosition + "px";
theThing.style.top = yPosition + "px";
}
// Helper function to get an element's exact position
function getPosition(el) {
var xPos = 0;
var yPos = 0;
while (el) {
if (el.tagName == "BODY") {
// deal with browser quirks with body/window/document and page scroll
var xScroll = el.scrollLeft || document.documentElement.scrollLeft;
var yScroll = el.scrollTop || document.documentElement.scrollTop;
xPos += (el.offsetLeft - xScroll + el.clientLeft);
yPos += (el.offsetTop - yScroll + el.clientTop);
} else {
// for all other non-BODY elements
xPos += (el.offsetLeft - el.scrollLeft + el.clientLeft);
yPos += (el.offsetTop - el.scrollTop + el.clientTop);
}
el = el.offsetParent;
}
return {
x: xPos,
y: yPos
};
}
</script>
</body>
</html>
Refer : MouseEvent clientX Property & Getting the Mouse Click Position
I want to restrict the area that an image can be drawn on a canvas. I am using Fabric.js.
I tried the following link, but it didn't work for me. Set object drag limit in Fabric.js.
I want any part of the image that would be drawn outside the red rectangle (pictured below) to just not be drawn.
var canvas = new fabric.Canvas('canvas');
$("#canvascolor > input").click(function() {
canvas.setBackgroundImage(this.src, canvas.renderAll.bind(canvas), {
crossOrigin: 'anonymous'
});
});
// trigger the first one at startup
$("#canvascolor > input:first-of-type()")[0].click();
document.getElementById('file').addEventListener("change", function(e) {
var file = e.target.files[0];
var reader = new FileReader();
console.log("reader " + reader);
reader.onload = function(f) {
var data = f.target.result;
fabric.Image.fromURL(data, function(img) {
var oImg = img.set({
left: 140,
top: 100,
width: 250,
height: 200,
angle: 0
}).scale(0.9);
oImg.lockMovementX = true;
canvas.add(oImg).renderAll();
var a = canvas.setActiveObject(oImg);
var dataURL = canvas.toDataURL({
format: 'png',
quality: 0.8
});
});
};
reader.readAsDataURL(file);
});
document.querySelector('#txt').onclick = function(e) {
e.preventDefault();
canvas.deactivateAll().renderAll();
document.querySelector('#preview').src = canvas.toDataURL();
};
canvas {
border: 1px solid black;
}
#canvascolor input {
height: 50px;
width: 50px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://rawgit.com/kangax/fabric.js/master/dist/fabric.min.js"></script>
<input type="file" id="file">
<canvas id="canvas" width="520" height="520"></canvas>
<section id="canvascolor">
<input class="canvasborder" type="image" src="https://dl.dropboxusercontent.com/s/9leyl96qd3tytn8/tshirt-front.jpg">
<input class="canvasborder" type="image" src="https://dl.dropboxusercontent.com/s/tk0fs5v4muo6898/tshirt-back.jpg">
</section>
<button href='' id='txt' target="_blank">submit</button><br/>
<img id="preview" />
I extended your script to answer you.
For your use case it looks like you could use some clipping function and uniform centered scaling.
Anyway the main question about restricting area for drawing is this:
var canvas = new fabric.Canvas('canvas');
$("#canvascolor > input").click(function() {
canvas.setBackgroundImage(this.src, canvas.renderAll.bind(canvas), {
crossOrigin: 'anonymous'
});
});
// trigger the first one at startup
$("#canvascolor > input:first-of-type()")[0].click();
function clipTShirt(ctx) {
ctx.save();
ctx.setTransform(1, 0, 0, 1, 0, 0);
var x = 140, y = 100 ,w = 225, h = 300;
ctx.moveTo(x, y);
ctx.lineTo(x + w, y);
ctx.lineTo(x + w, y + h);
ctx.lineTo(x, y + h);
ctx.lineTo(x, y);
ctx.restore();
}
document.getElementById('file').addEventListener("change", function(e) {
var file = e.target.files[0];
var reader = new FileReader();
console.log("reader " + reader);
reader.onload = function(f) {
var data = f.target.result;
fabric.Image.fromURL(data, function(img) {
var oImg = img.set({
left: 140,
top: 100,
width: 250,
height: 200,
angle: 0,
lockUniScaling: true,
centeredScaling: true,
clipTo: clipTShirt
}).scale(0.9);
oImg.lockMovementX = true;
canvas.add(oImg).renderAll();
var a = canvas.setActiveObject(oImg);
var dataURL = canvas.toDataURL({
format: 'png',
quality: 0.8
});
});
};
reader.readAsDataURL(file);
});
document.querySelector('#txt').onclick = function(e) {
e.preventDefault();
canvas.deactivateAll().renderAll();
document.querySelector('#preview').src = canvas.toDataURL();
};
canvas {
border: 1px solid black;
}
#canvascolor input {
height: 50px;
width: 50px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://rawgit.com/kangax/fabric.js/master/dist/fabric.min.js"></script>
<input type="file" id="file">
<canvas id="canvas" width="520" height="520"></canvas>
<section id="canvascolor">
<input class="canvasborder" type="image" src="https://dl.dropboxusercontent.com/s/9leyl96qd3tytn8/tshirt-front.jpg">
<input class="canvasborder" type="image" src="https://dl.dropboxusercontent.com/s/tk0fs5v4muo6898/tshirt-back.jpg">
</section>
<button href='' id='txt' target="_blank">submit</button><br/>
<img id="preview" />
How can I use jQuery to follow the cursor with a DIV?
You can't follow the cursor with a DIV, but you can draw a DIV when moving the cursor!
$(document).on('mousemove', function(e){
$('#your_div_id').css({
left: e.pageX,
top: e.pageY
});
});
That div must be off the float, so position: absolute should be set.
You don't need jQuery for this. Here's a simple working example:
<!DOCTYPE html>
<html>
<head>
<title>box-shadow-experiment</title>
<style type="text/css">
#box-shadow-div{
position: fixed;
width: 1px;
height: 1px;
border-radius: 100%;
background-color:black;
box-shadow: 0 0 10px 10px black;
top: 49%;
left: 48.85%;
}
</style>
<script type="text/javascript">
window.onload = function(){
var bsDiv = document.getElementById("box-shadow-div");
var x, y;
// On mousemove use event.clientX and event.clientY to set the location of the div to the location of the cursor:
window.addEventListener('mousemove', function(event){
x = event.clientX;
y = event.clientY;
if ( typeof x !== 'undefined' ){
bsDiv.style.left = x + "px";
bsDiv.style.top = y + "px";
}
}, false);
}
</script>
</head>
<body>
<div id="box-shadow-div"></div>
</body>
</html>
I chose position: fixed; so scrolling wouldn't be an issue.
This works for me. Has a nice delayed action going on.
var $mouseX = 0, $mouseY = 0;
var $xp = 0, $yp =0;
$(document).mousemove(function(e){
$mouseX = e.pageX;
$mouseY = e.pageY;
});
var $loop = setInterval(function(){
// change 12 to alter damping higher is slower
$xp += (($mouseX - $xp)/12);
$yp += (($mouseY - $yp)/12);
$("#moving_div").css({left:$xp +'px', top:$yp +'px'});
}, 30);
Nice and simples