473,672 Members | 2,881 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Zorn's DragDrop Library - swapImage() function query

6 New Member
I'm using the great dd.dragdrop Library for many parts of the page, including loading in a user selected image via the "swapImage( )" function.

Expand|Select|Wrap|Line Numbers
  1. var imgRe = /^.+\.(jpg|jpeg|gif|png)$/i;
  2.  
  3. function swapMe(pathField, previewName)
  4. {       
  5.     var path = pathField.value;
  6.     if (path.search(imgRe) != -1)
  7.     {   
  8.         dd.elements.replaceMe.swapImage('file://'+path) ;
  9.     }       
  10.     else    
  11.     {   
  12.         alert("JPG, PNG, and GIFs only!");
  13.     } 
  14. }
  15.  
All works, but ...

The incoming image inherits the size and aspect ratio of the 'image' used to hold a place for it as required by the library.

<IMG Name="replaceMe " SRC="blank.gif" width="630" Height="730"><B R>

I have no control over the actual size of the user's image, although it will be within a moderate range of 630w x 730h.

Part of the use requires sizing to fit a measuring device. I would prefer to use SCALABLE, but because of the inheritance of the holder's height/width, the user has to H and V size independently with RESIZABLE.

Query: Is there an easy manner to get the incoming image to retain its aspect ratio - even IF it needs a greater/lesser SCALABLE adjustment?

Thanks,
Dec 28 '07 #1
11 1979
acoder
16,027 Recognized Expert Moderator MVP
Welcome to TSDN!

Preload the image and get its height and width and set replaceMe's properties to these:
Expand|Select|Wrap|Line Numbers
  1. var img = new Image();
  2. img.src = path;
  3. var width = img.width;
  4. var height = img.height;
Dec 29 '07 #2
trackside
6 New Member
Thank you, but this seems to fail loading the H&V values into the necessary "resizeTo() " function.

I'm using the dragdrop Library for swapping, scaling and moving the image. I can't seem to access the "replaceMe1 " image's H&W directly, but need to use the Library's "resizeTo() " function. Hard numbers work;
dd.elements.rep laceMe1.resizeT o(630, 730);
this script using variables doesn't!

Expand|Select|Wrap|Line Numbers
  1.         myPic = new Image();
  2.         myPic.src = path;
  3.         var myWidth =  myPic.width;
  4.         var myHeight = myPic.height;
  5.         dd.elements.replaceMe1.swapImage('file://'+path) ;
  6.         dd.elements.replaceMe1.resizeTo(myWidth, myHeight);
  7.  
(Sorry, used carets, not brackets first post! Hope this is right for separating code.)

Directly trying "myPic.widt h" in the function "resizeTo() " also fails to swap an image.

JA
Dec 29 '07 #3
acoder
16,027 Recognized Expert Moderator MVP
You may need to make sure that the image has fully loaded. Are the values of myWidth and myHeight correct?
Dec 29 '07 #4
trackside
6 New Member
The problems lies in line 2 of your suggestion.

As it's always a local file, this needs to be:
myPic.src = 'file://'+path;

This works, excepting some images which appear not to have their H&W attributes within the file.

Nearly there <G>!

Thanks,
JA
Dec 29 '07 #5
acoder
16,027 Recognized Expert Moderator MVP
Most likely the problem is that the image has not yet fully loaded which means that the width and height properties are not available yet. If it works sometimes, it may mean that the image is cached already, so the values are available immediately.

Use the img.complete property (non-standard, but seemingly well-supported) to check if the image has completed loading.
Dec 29 '07 #6
trackside
6 New Member
I'm not familiar with the img.complete property, so did a quick Google and found many messages that it returns errors in Opera and Safari.

Interestingly, the latest version seems to work with anything I've yet tried. NO image has yet gone to the 'default' values per the DOM Inspector.

Expand|Select|Wrap|Line Numbers
  1. function swapMe(pathField, previewName)
  2. {       
  3.     var path = pathField.value;
  4.     if (path.search(imgRe) != -1)
  5.     {   
  6.         myPic = new Image();
  7.         myPic.src = 'file://'+path;
  8.         var myWidth =  myPic.width;
  9.         var myHeight = myPic.height;
  10.         if (myWidth == 0)
  11.         { 
  12.             myWidth = 280;
  13.             myHeight = 325;
  14.          }else{
  15.             myWidth = myWidth*.4;
  16.             myHeight = myHeight*.4;
  17.         }
  18.         dd.elements.replaceMe1.swapImage('file://'+path) ;
  19.         dd.elements.replaceMe1.resizeTo(myWidth, myHeight);
  20.     }       
  21.     else    
  22.     {   
  23.         alert("JPG, PNG, and GIFs only!");
  24.     } 
  25. }
  26.  
I found a simple additional function that seems to work - a noticeable delay in the load. That should have given the image enough time to load when called immediately after the .src setting (line 7) with pausecomp(x)

Expand|Select|Wrap|Line Numbers
  1. function pausecomp(millis)
  2. {
  3. var date = new Date();
  4. var curDate = null;
  5.  
  6. do { curDate = new Date(); }
  7. while(curDate-date < millis);
  8. }
  9.  
However, if I remove the width 'test' it once again fails loading and H&W are 'null' on many (all?) images.

Advise? Add in the delay or just go with the above (swapImage) code that seems to work on all images.

Thanks for the help!

JA
Dec 29 '07 #7
acoder
16,027 Recognized Expert Moderator MVP
Instead of a delay function, use setInterval to check the image has loaded. There is also the option of onload, but that's also not standard.
Dec 29 '07 #8
trackside
6 New Member
Thanks.

Took a bit, but installed that 'complete' process in the code with the same result as the 'delay'. Slight pause, then images load.

Take out the 'width' test (line 10 & on) in swapMe function and images fail with H&W's of "0".

As it works, seemingly without fail, with the 'width' test in and no 'delay' or 'complete' coding, I'll leave it there (for now <G>!).

JA
Dec 31 '07 #9
acoder
16,027 Recognized Expert Moderator MVP
With the 'width test', does it not always set the height and width to 325 and 280?

Anyway, glad to hear that you've got something that works. Post again if you have more questions and we'll try and answer them.
Dec 31 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

7
5838
by: Kate | last post by:
Hi: I have a form with a picture box and some command buttons to make certain shapes appear in the picture box. The shapes are drawn on blank UserControls added like this: 'at top of form module Dim WithEvents tc As testControl 'button1_click (for example)
2
2331
by: dave | last post by:
I am wishing to drag something from the desktop to a tab of a tab control in vb.net. The dragdrop event fires when release the mouse above the tab however i am having a hard time trying to figure out which tab it was dropped on. Currently i am using the following code but it is not working..Any help would be appreciated... Private Sub tc_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles...
2
2202
by: fochie | last post by:
Greetings, First time append. I have a dynamic page that can potentially contain thousands of links. Each link has an onmouseover specified with Walter Zorn's Tool Tip feature to pop up a small image. This has been working fine but I noticed that a page with many links loads very slowly. I then realized that the images that are used for the onmouseover pop ups are all automatically being downloaded when the page initially loads.
3
2433
by: Dan | last post by:
How do I find out what control a DragDrop event comes from? I initially presumed that it was the "sender" parameter. But this always seems to be the destination. Ie ... private void listviewMonday_DragDrop (object sender, System.Windows.Forms.DragEventArgs e) { // "sender" always seems to point to "listviewMonday" here.
3
3786
by: Gary Dunne | last post by:
I'm writing an app that requires drag and drop operation between a ListView and a TreeView control. (The source is the ListView). During the drag drop operation I want to be able to detect the target node in the treeview and auto expand it if applicable... but after a fair bit of head scratching I can't find any easy way to accomplish this. I think what i really need is the equivalent of the HitTest method from the COM version of the...
8
7011
by: Joel Byrd | last post by:
I'm trying to accomplish the following: I want to set up generic drag-drop functionality for dragging and dropping images. I want to be able to drag various images into various drop targets, and when they are dropped, they should call a function that I've defined. I searched the internet for a tool that could accomplish my goal easily and effectively, and I found 2. Yahoo! UI Library and Walter Zorn's wz_dragdrop. At first, the...
0
8418
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8940
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8840
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8630
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6251
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4237
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2833
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2084
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1831
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.