Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Javascript not an option? #27

Open
ChrisGitIt opened this issue May 20, 2013 · 2 comments
Open

Javascript not an option? #27

ChrisGitIt opened this issue May 20, 2013 · 2 comments

Comments

@ChrisGitIt
Copy link

Hi,

i just wonder if using javascript within the svg is not an option? Instead of creating SVGs for all images your can use just one SVG to clowncar dynamically. For example the embedded SVG in html could look like this:

<object data="object/dynamic.svg?img=my-image"><!-- IE fix stuff ... --></object>

and the SVG would look like this (quick mock up):

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 300 329" preserveAspectRatio="xMidYMid meet">
  <title>Clown Car Technique</title>

  <style>
    svg {
      background-size: 100% 100%;
      background-repeat: no-repeat;
    }
  </style>
    <script type="application/ecmascript">
<![CDATA[

// get the GET Parameter from the request url string
function getQueryParameter(name){
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");  
  var regexS = "[\\?&]"+name+"=([^&#]*)";  
  var regex = new RegExp( regexS );  
  var results = regex.exec( window.location.href ); 
  return results ? results[1] : "";
}

// set the background image
function setBackgroundImage(size) {
  this.document.firstChild.setAttribute("style", "background-image: url('../"+getQueryParameter('img')+"/"+size+".png')");
}

// default size
var size = 'huge';

// media queries equivalent?
if(this.innerWidth < 400) {
  size = 'small';
} else if(this.innerWidth < 700) {
  size = 'medium';
} else if(this.innerWidth < 1000) {
  size = 'big';
}
// read ...
setBackgroundImage(size);

]]>
    </script>
</svg>

I haven't tested the browser caching possibility for "dynamic.svg" to not be loaded over and over again. Beside that, a minified (and zipped?) version is less than one KB ...

By the way: the image set (from small to large image) is within the folder "my-image".

The reload while resizing the browser window could be done via JS event "window.onresize" or something like this.

Greets Chris

@estelle
Copy link
Owner

estelle commented Jun 11, 2013

The goal of the technique is to be a viable responsive solution for . is presenation. javascript is behavior. We can already replace the src with JavaScript, so might as well use js to write the correct path for the right size image. CCT aims to find a non-JS solution.

@knightdr
Copy link

@ChrisGitIt @estelle

Would something like this(http://www.w3.org/TR/2009/WD-SVGParamPrimer-20090430/#URLAndObjectParameters) get closer to what you are looking for.

I don't know that it would set the path within the style block but worth a look perhaps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants