cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <div class="wrapper">

  <div class="image-holder">
    
    <img class="main-image" src="http://www.pseudosuede.com/2015running/images/map_background.png" alt="2015 Running Data Image.">
    
    <div class="relative-box">
      <div class="relative-locator">
      </div>
    </div>
    
  </div> <!-- .image-holder -->
  
  
  
</div>
<div class="text-feedback">
<p class="natural-img-width">Nat. Img. W 2880px</p>
<p class="natural-img-height">Nat. Img. H 1700px</p>
<p class="calculated-img-width"></p>
<p class="calculated-img-height"></p> 
<p class="img-shell-width"></p>
<p class="img-shell-height"></p> 
<p class="loc-box-top"></p>
<p class="loc-box-left"></p> 
<p class="loc-box-prcnt-left"></p>
<p class="loc-box-prcnt-top"></p>
<p class="img-drag-left"></p>
<p class="img-drag-top"></p>
<p class="img-stop-left"></p>
<p class="img-stop-top"></p>
<p class="img-prcnt-left"></p>
<p class="img-prcnt-top"></p>
</div>
            
          
!
            
              * { -moz-box-sizing: border-box; -webkit-box-sizing: border-box; box-sizing: border-box; }

body {
  background-color:#DBD06C;
}

.text-feedback {
  position:absolute;
  top:5px;
  left:5px;
}

.wrapper {
  padding:10em;
  width:100%;
  margin:0 auto;
}

.image-holder {
  width:100%;
  height:600px;
  overflow:hidden;
  position:relative;
  background-color:rgba(0,0,0,0.10);
  img {
    display:block;
    position:relative;
    top:0;
    left:0;
    /*width:100%;*/ // Remove This to begin upper left image finder.
    cursor: -webkit-grab; cursor:-moz-grab;
      &:active {
        cursor: -webkit-grabbing; cursor:-moz-grabbing;
      }
  }
}

.relative-box {
  display:inline-block;
  position:absolute;
  top:1em;
  left:1em;
  width:200px;
  height:100px;
  background-color:rgba(0,0,0,.25);
  border-color:rgba(0,0,0,0.35);
  border-width:3px;
  border-style:solid;
}

.relative-locator {
  position:relative;
  top:0;
  left:0;
  width:50px;
  height:25px;
  background-color:rgba(255,255,255,.25);
  border-color:rgba(255,255,255,0.35);
  border-width:3px;
  border-style:solid;
  cursor: -webkit-grab; cursor:-moz-grab;
  &:active {
    cursor: -webkit-grabbing; cursor:-moz-grabbing;
  }
}
            
          
!
            
              // New Questions
// 1. Can I create a reusable function for the ideas in the .main-image draggable area. Perhaps I can use the traditional $().position instead of ui.position....
// 2. Do I need to split up the functions even more?

function getRelativeLocatorPosition() {
  var locBoxT = $('.relative-locator').position().top;
  var locBoxL = $('.relative-locator').position().left;

  $('p.loc-box-top').html("Loc. Top Value " + locBoxT + "px");
  $('p.loc-box-left').html("Loc. Left Value " + locBoxL + "px");

  return this;
}

function imageCalculation() {
  var calcImgW = $('.main-image').outerWidth();
  var calcImgH = $('.main-image').outerHeight();

  var calcShellW = $('.image-holder').outerWidth();
  var calcShellH = $('.image-holder').outerHeight();

  var imgProportion = calcImgH / calcImgW;
  var shellProportion = calcShellH / calcShellW;

  var relBoxW = $('.relative-box').outerWidth();
  var relBoxH = relBoxW * imgProportion;

  var locProportionW = calcShellW / calcImgW;
  var locProportionH = calcShellH / calcImgH;

  var borderThickness = 6; // For Each of the Boxes Borders

  var locBoxW = (relBoxW * locProportionW) - borderThickness;
  var locBoxH = (relBoxH * locProportionH) - borderThickness;
  var locBoxT = $('.relative-locator').position().top;
  var locBoxL = $('.relative-locator').position().left;

  var locBoxPercentageLeft = (locBoxL / (relBoxW - locBoxW - borderThickness)) * 100;
  var locBoxPercentageLeftRounded = Math.ceil(locBoxPercentageLeft);
  var locBoxPercentageTop = (locBoxT / (relBoxH - locBoxH - borderThickness)) * 100;
  var locBoxPercentageTopRounded = Math.ceil(locBoxPercentageTop);

  $('.relative-box').css("height", relBoxH);

  $('.relative-locator').css("width", locBoxW);
  $('.relative-locator').css("height", locBoxH);

  $('p.calculated-img-width').html("Calc. Img W " + calcImgW + "px");
  $('p.calculated-img-height').html("Calc. Img H " + calcImgH + "px");

  $('p.img-shell-width').html("Img Shell W " + calcShellW + "px");
  $('p.img-shell-height').html("Img Shell H " + calcShellH + "px");

  $('p.loc-box-prcnt-left').html("Loc. Left " + locBoxPercentageLeftRounded + "%");
  $('p.loc-box-prcnt-top').html("Loc. Top " + locBoxPercentageTopRounded + "%");

  return this;
}

$(function () {
  $('.relative-locator').draggable({
    containment: "parent",
    start: function() {
      getRelativeLocatorPosition();
      imageCalculation();
    },
    drag: function() {
      getRelativeLocatorPosition();
      imageCalculation();
    },
    stop: function() {
      getRelativeLocatorPosition();
      imageCalculation();
    }
  });
});

$(function() {
  $(".main-image").draggable({
    start: function() { },
    drag: function(event, ui) {
      var left = ui.position.left,
        offsetWidth = ($(this).width() - $(this).parent().width()) * -1,
        top = ui.position.top,
        offsetHeight = ($(this).height() - $(this).parent().height()) * -1;

      /*Begin Repeated Items, Fix If Successful*/
      /*var calcImgW = $('.main-image').outerWidth();
  var calcImgH = $('.main-image').outerHeight();

  var calcShellW = $('.image-holder').outerWidth();
  var calcShellH = $('.image-holder').outerHeight();*/
      /*End Repeated Items, Fix If Successful*/

      /*var imgBoxPercentLeft = (offsetWidth / (calcShellW - calcImgW)) * 100;
      var imgBoxPercentLeftRounded = Math.ceil(imgBoxPercentLeft);*/
      /*var locBoxPercentageTop = (locBoxT / (relBoxH - locBoxH - borderThickness)) * 100;
      var locBoxPercentageTopRounded = Math.ceil(locBoxPercentageTop);*/

      if (left > 0) {
        ui.position.left = 0;
      }
      if (offsetWidth > left) {
        ui.position.left = offsetWidth;
      }
      if (top > 0) {
        ui.position.top = 0;
      }
      if (offsetHeight > top) {
        ui.position.top = offsetHeight;
      }

      $('p.img-drag-left').html("Img. Drag Left " + offsetWidth);
      $('p.img-drag-top').html("Img. Drag Top " + offsetHeight);

      /*$('p.img-prcnt-left').html("Img. Loc. Left " + imgBoxPercentLeftRounded + "%");*/
      /*$('p.img-prcnt-top').html("Img. Loc. Top " + imgBoxPercentageTopRounded + "%");*/

    }, // drag: function
    stop: function(event, ui) {
     var left = ui.position.left
        var top = ui.position.top
        $('p.img-stop-left').html("Img. Stop Left " + left);
        $('p.img-stop-top').html("Img. Stop Top " + top);
      } // stop: function
  }); // .draggable
}); // function

$(window).load(function() {
  imageCalculation();
  getRelativeLocatorPosition();
});
$(window).resize(function() {
  imageCalculation();
  getRelativeLocatorPosition();
  // Function to make sure that when the image is dragged all the way to the right, and then the page is made wider, that the image also moves with it and the image wrapper background isn't exposed.
  // Function to make sure that when the Relative-Locator is in the lower right corner, and the screen is narrow and then dragged to a wider position, that the Relative-Location doesn't escape the Relative-Box.
});

// Research:

//http://stackoverflow.com/questions/318630/get-real-image-width-and-height-with-javascript-in-safari-chrome

//https://learn.jquery.com/plugins/basic-plugin-creation/
//http://stackoverflow.com/questions/15665231/run-jquery-function-on-window-events-load-resize-and-scroll
// -- Maybe I need to break down each individual task into it's own function so that I can call a specific class for each of the functions, like the above link shows.

//https://jqueryui.com/draggable/
//http://stackoverflow.com/questions/20635918/containment-issue-with-draggable-element-within-a-resizable-container
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console