I'd like to know if you have any idea on how I could click and drag on a canva to create a div.
Like in a video-game where you drag & drop to create an area.
Is there any plugin or things that were made in order to achieve that?
Thanks for your replies.
Building on Ludwig's snippet ... this will draw some squares on the page using mousedown and mouseup event listeners :)
EDIT: Had too much fun playing with this so here is an updated version with a select box to help you draw the selection, and now working in all directions :) I'd still recommend using a library like this one (https://github.com/Simonwep/selection) though, especially if you want touchscreen support.
// declare some global variables
let showSelection = false;
let Xstart;
let Ystart;
$(document).ready(function(){
$(document).mousedown(function(event){
// set a variable that indicates the user is drawing a selection
showSelection=true;
// save the position where the mouse click started
Xstart=event.pageX;
Ystart=event.pageY;
// create the select box element and give it some starting positions
$('<div id="selection"></div>')
.css({
"left": Xstart + 'px',
"top": Ystart + 'px',
})
.appendTo(document.body);
});
$(document).mousemove(function(event){
// if we are making a selection lets keep updating our select box dimensions
if(showSelection == true) {
// horizontal selection
if (event.pageX > Xstart) {
// left to right
$('#selection').css({
"width": event.pageX-Xstart + 'px'
})
} else {
// right to left
$('#selection').css({
"left": event.pageX,
"width": Xstart-event.pageX + 'px'
})
}
// vertical selection
if (event.pageY > Ystart) {
// top to bottom
$('#selection').css({
"height": event.pageY-Ystart + 'px'
})
} else {
// bottom to top
$('#selection').css({
"top": event.pageY,
"height": Ystart-event.pageY + 'px'
})
}
}
});
$(document).mouseup(function(event){
// we can borrow the needed values from the #selection element to draw our 'area' :)
LeftVal = $("#selection").css('left')
TopVal = $("#selection").css('top')
WidthVal = $("#selection").css('width')
HeightVal = $("#selection").css('height')
// create the area element and give it the dimensions
$('<div class="area"></div>')
.css({
"left": LeftVal,
"top": TopVal,
"width": WidthVal,
"height": HeightVal
})
.appendTo(document.body);
// get rid of the selection element from the DOM
$( "#selection" ).remove();
// and set our showSelection variable back to false
showSelection=false;
});
});
.area {
position: absolute;
background-color:lightgray;
}
#selection {
position: absolute;
background-color: lightblue;
border: 2px solid blue;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
You could use this:
https://simonwep.github.io/selection/
In combination with something like this:
$(document).ready(function(){
$(document).click(function(event){
$('<div id="kenobi">Hello there</div>')
.css({
"left": event.pageX + 'px',
"top": event.pageY + 'px'
})
.appendTo(document.body);
});
});
body {
position: relative;
}
#kenobi {
position: absolute;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Related
I want my "Back to top" button to appear when the user scrolls about half down the webpage not immediately once the web page shows up. How would you be able to do that using HTML and CSS?
Element I want to appear about half way down and click to go back to the top:
<img id="upArrow" src="Images/arrow-up.png" alt="Up arrow">
Let me know if this works:
Edit: Added animation to return user to top on .click
<script>
$(window).scroll(function () {
if ($(window).scrollTop() > $('body').height() / 2) {
$('#upArrow').fadeTo(500, 1);
}
});
$('#upArrow').click(function(){
$("html, body").animate({ scrollTop: 0 }, "slow");
});
</script>
Set your upArrow as:
<img id="upArrow" style="opacity: 0;" src="Images/arrow-up.png" alt="Up arrow"/>
Alternative .js:
$(document).ready(function(){
$(window).on('scroll', function() {
var y_scroll_pos = window.pageYOffset;
var scroll_pos_test = 500 // Edit this number to define how far down the page the div fades in.
if(y_scroll_pos > scroll_pos_test) {
$('#upArrow').fadeTo(500, 1);
}
});
});
$('#upArrow').on("click",function(){
$("html, body").animate({ scrollTop: 0 }, "slow");
});
I am trying making a photo frame pattern on canvas. In which I want to put two, three or maybe more photos under an overlay frame. Where few parts of the overlay frame are transparent.
If I upload photo1 into first section of frame its visible into second section also and uploading photo2 into section two is also visible into first section. Depending on which photo is uploaded first or edited at last is overlapping the other photo.
I want to know how to hide the overflow of photo so it should not be visible into other sections. How can I achieve this?
I have done this so far:
canvas.on({
'object:moving': onChange,
'object:scaling': onChange,
'object:rotating': onChange,
});
function onChange(options) {
options.target.setCoords();
canvas.forEachObject(function (obj) {
if (obj === options.target)
return;
if (obj.id != 'cover1' && obj.id != 'cover2')
return;
if (options.target.intersectsWithObject(obj)) {
// Hide this portion
canvas.renderAll();
}
});
}
Kindly provide me the best solution
You need to apply a clipTo() method onto the image.
var canvas;
$(function(){
canvas = window._canvas = new fabric.Canvas('canvas');
var radius = 200;
fabric.Image.fromURL('http://fabricjs.com/assets/pug_small.jpg', function(img) {
img.scale(0.5).set({
left: 250,
top: 250,
angle: -15,
clipTo: function (ctx) {
ctx.arc(0, 0, radius, 0, Math.PI * 2, true);
}
});
canvas.add(img).setActiveObject(img);
});
});
This fiddle show the technique
Fiddle
i'm working on a website and wondering if there is any code that I can add to a "Fixed in Window" Item that will be visible once a visitor has scrolled 200 Pixels, for example.
Couldn't find anything specifically for this in any other questions.
Cheers
My bad, I found this, perhaps this is what I'm looking for Use jQuery to show a div only when scroll position is between 2 points. Correct me if I'm wrong :-)
You can detect scrolling position with use of j Query and then if scroll is moved hide your div. Below given is example code. please review it and work on it little bit according to your requirement it is working fine with me.
<script type='text/javascript' src='http://code.jquery.com/jquery-1.7.1.js'></script>
<script type='text/javascript'>//<![CDATA[
$(window).load(function(){
$(function() {
$.fn.scrollBottom = function() {
return $(document).height() - this.scrollTop() - this.height();
};
var $el = $('#sidebar>div');
var $window = $(window);
var top = $el.parent().position().top;
$window.bind("scroll resize", function() {
var gap = $window.height() - $el.height() - 10;
var visibleFoot = 172 - $window.scrollBottom();
var scrollTop = $window.scrollTop()
if (scrollTop < top + 10) {
$el.css({
top: (top - scrollTop) + "px",
bottom: "auto"
});
} else if (visibleFoot > gap) {
$el.css({
top: "auto",
bottom: visibleFoot + "px"
});
} else {
$el.css({
//use your css property here if you want to display none a div
display: none,
bottom: "auto"
});
}
}).scroll();
});
});//]]>
</script>
here is my code:
$('myButton').addEvents({
mouseenter: function(){
$('myImage').setStyle('display','block');
$('myImage').morph({
'duration': 500,
'transition': Fx.Transitions.Sine.in,
'opacity': 1,
'top': -205
});
},
mouseleave: function(){
$('myImage').morph({
'opacity': 0,
'top': -175,
'onComplete': hidemyImage
});
}
});
function hidemyImage() {
$('myImage').setStyle('display','none')
}
the onComplete inside the mouseleave does not work as expected... it hides the image immediately when i move away from myButton instead of hiding it after the morph has finished... i tried several solutions but none worked so far. any idea / help? thanks in advance!
you need to work with the instance and not pass on things in the morph function directly, that takes properties to morph and it probably runs your function immediately in the hope it will return a property value. you can do el.set('morph', {onComplete: hideImagefn}) before that and it will work but read on...
one way to do it is to set your morph options/instance once and work with it afterwards like so:
(function() {
var img = document.id('myImage').set('morph', {
duration: 500,
transition: Fx.Transitions.Sine.in,
link: 'cancel',
onStart: function() {
this.element.setStyle('display', 'block');
}
}), fx = img.get('morph');
// of you can just do var fx = new Fx.Morph(img, { options});
document.id('myButton').addEvents({
mouseenter: function(){
fx.start({
opacity: 1,
top: -205
});
},
mouseleave: function(){
fx.addEvent('complete', function() {
this.element.setStyle('display', 'none');
this.removeEvents('complete');
}).start({
opacity: 0,
top: -175
});
}
});
})();
the start ensures its always visible when mouseovered, the link is cancel which means it will stop execution if you mouse out too early and if you do mouseout, it will then hide the image and remove the onComplete event so that if you show it once more, it stays visible when it comes into view.
if you don't plan on being able to bring it back you can clean-up better and even use event pseudos like onComplete:once etc - though thats part of Event.Pseudos from mootools-more.
in general - .get/.set morph is your setup. el.morph() passes values to morphInstance.start()
play here: http://jsfiddle.net/dimitar/NkNHX/
I'm creating an interactive app allowing people to customise doors. To allow them to select the letterbox I want to show it when they hover over the door and remove it when they hover out. This works fine, but when I hover the letter box the doors 'hover out' is fired.
This causes a strange flickering effect.
I have created a jsfiddle here showing this effect
Just wondering if anyone has a solution to this?
I basically need the letterbox to stay in place when the user hovers the door, I also need a click state for both the door and letterbox.
not sure if this is the most efficient method, but it works. jsfiddle
var doorClickState, letterbox, inletterbox = false;
$(function() {
var paper = Raphael("canvas", 330, 457);
//draw the door
doorClickState = paper.path("M0,0v200h117V0H0z").translate(0, 0).attr({
fill: "#FF0000",
stroke: 0,
opacity: 0.9
}).toFront();
//draw and hide letterbox
letterbox = paper.path("M0,0v15h60V0H0z").translate(30, 100).attr({
fill: "#000000",
stroke: 0,
opacity: 0.9
}).hide();
//click states for both
doorClickState.click(function() {
alert('door clicked');
});
letterbox.click(function() {
alert('letterbox clicked');
});
doorClickState[0].onmouseover = function() {
letterbox.show();
}
letterbox[0].onmouseover = function() {
inletterbox = true;
}
letterbox[0].onmouseout = function() {
inletterbox = false;
}
doorClickState[0].onmouseout = function() {
setTimeout(function() {
if (!inletterbox) {
letterbox.hide();
}
}, 20);
};
});