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

replaceImage method as requested in #32 #35

Merged
merged 13 commits into from
Jul 8, 2017

Conversation

ccpandhare
Copy link
Collaborator

What Changes this PR Contains

  • Created the method replaceImage
  • Small bugfix in Run.js
  • Updated Readme.md
  • Unit Test for replaceImage

replaceImage Syntax

sequencer.replaceImage('image_name','new_src');

@ccpandhare
Copy link
Collaborator Author

Should I merge this Is, @jywarren ?

@ccpandhare ccpandhare requested a review from jywarren July 4, 2017 18:07
@jywarren
Copy link
Member

jywarren commented Jul 4, 2017

I think we need to say "imageId" instead and use getElementById, and why do we need newSrc? Shouldn't it just run the sequencer on the given image and replace it with the output?

Thanks!!!

@ccpandhare
Copy link
Collaborator Author

I am afraid not...
This method replaces the initial image in the sequencer, resulting in change in all images.
So we do need a new image for this, right?

@jywarren
Copy link
Member

jywarren commented Jul 5, 2017

OK, so what i mean is, if you have an image on a webpage, what's everything you need to do in ImageSequencer to have a modified image replace that image? You need the img element's id so you can find it, and then you need some steps to run on it. The final state of hte image (after all the steps have run) should then be inserted back into the webpage, replacing the original.

I notice, with chaining methods, (#40), you could do:

$('#myImage').replace(sequencer.addStep(...).run($('#myImage')[0]));
// but an example not in jQuery, maybe

@ccpandhare
Copy link
Collaborator Author

ccpandhare commented Jul 5, 2017 via email

@ccpandhare
Copy link
Collaborator Author

ccpandhare commented Jul 5, 2017 via email

@ccpandhare
Copy link
Collaborator Author

Now I do get what you were saying.

@jywarren
Copy link
Member

jywarren commented Jul 5, 2017

Right, not for node -- and this is a slightly different use of IS -- the only state would be the steps, not the image. So we use it as if it were a filter for any given image. I guess in that usage, we might not want to actually store the image, or affect any images already loaded into the sequencer -- this is more just a useful way to use a sequencer that's already been set up, to do things on a webpage.

@ccpandhare
Copy link
Collaborator Author

So do you mean that a separate mode should be made in which the intermediate images aren't stored? Or maybe they're deleted shortly after?
Actually, I am still not clear on your interpretation of replaceImage.

Is it a shorthand for $('#myImage').replace(sequencer.addStep(...).run($('#myImage')[0])); ?

Is it a shorthand for sequencer.images[image].steps[0].draw(NEW_IMAGE) ?

What exactly does it replace? (an image with an image in the DOM) or (an image with an image in the ImageSequencer) ?

@jywarren
Copy link
Member

jywarren commented Jul 5, 2017

I think we could leave the intermediate images in place and if we never use the sequencer instance again, they'll just get abandoned, which for this use is fine.

It is a shorthand for the first option -- the intention being to take an image from the DOM, run steps on it, and insert it back into the DOM where the original was. Imagine a use case -- you want to use IS to make every image in a web-based gallery monochrome. You could run (roughly):

$('img.galleryImages').each(function(image) {
  var s = new ImageSequencer();
  s.addStep('monochrome').run(image, function onComplete(output) { $(image).replace(output) });
});

or in shortened syntax (and for just one image, in this case) something really minimal, like:

ImageSequencer.replaceImage('img#myImage', steps);

I'm actually agnostic about whether we should use an existing instance of sequencer with steps already in it, or we should require steps to be added, and treat this as a kind of "quick constructor" -- that's up to you. I am mostly thinking about a convenient and compact use case and how to encourage people to easily use ImageSequencer in their web-based projects.

Imagine wanting to make a quick JS utility to crop every image on a page, or generate thumbnails, or whatever -- what's the simplest and most straightforward way for IS to help people do this?

@ccpandhare
Copy link
Collaborator Author

Wow! That is a great insight, I must say!
Now this is clear to me. Thanks!

@jywarren
Copy link
Member

jywarren commented Jul 5, 2017

Super, and sorry if I was mixing up the architectural question of state and so forth with the real desire which was just to increase adoption with an easy to use and short function! :-)

@ccpandhare
Copy link
Collaborator Author

No, what you said is perfectly fine!
Thanks!

@ccpandhare
Copy link
Collaborator Author

Latest Changes

  • I have created the replaceImage method.
  • Added this update to README.md
  • Created a small working demo at "replace-demo.html"

if(url.substr(0,11).toLowerCase()!="data:image/") xmlHTTP.send();
else make(url);

function make(url) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the pros/cons of using this workaround here vs. just letting a canvas handle it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PROS:

  • May bypass some CORS restrictions.
  • Will also work on browsers not supporting Canvas. (read: IE < 9)

CONS:

  • An extra HTTP request each time.

I feel this needs more testing (to find out whether the premise of bypassing CORS restrictions is actually valid). What do you say?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. But I also like the nonrequirement of canvas, though of course some modules will decide to use it.

@jywarren
Copy link
Member

jywarren commented Jul 7, 2017

Cool, love the demo idea! Let's merge this as soon as we can as it's getting a bit complex and we really should be doing separate PRs for separate features.

Copy link
Member

@jywarren jywarren left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess i think we need to start testing LoadImage and ReplaceImage, and in general step up our testing, otherwise we'll get into a state where getting closer to full test coverage starts to seem overwhelming! What do you think? You could test that ReplaceImage changes the src of the images, you could actually resize them and then check their widths, maybe, to confirm that it happened? Like... could we test it with $('image').width() == 100 after or something.

Do we already test LoadImage?

Great work here! Exciting!

README.md Outdated
`selector` is a CSS selector. If it matches multiple images, all images will be
modified. `steps` may be the name of a module or array of names of modules.

Note: Local images will work only if they are in the same directory or a subdirectory.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

local images in the browser? why is this? is it relative path required? i.e. ../../ then we could say that. This is a bit mysterious sounding without an explanation.

Copy link
Collaborator Author

@ccpandhare ccpandhare Jul 8, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suppose
the HTML file is in /Users/PublicLab/Desktop/image-sequencer, also:
image1 : <img src="image.jpg">
image2 : <img src="images/image.jpg">
image3: <img src="../image.jpg">
image4: <img src="/Users/PublicLab/Desktop/image.jpg">

Then:
image1, image2 are allowed.
image3, image4 will throw a CORS Error on some browsers like those running on Gecko.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have updated README.md with a better explanation.

README.md Outdated
Note: Local images will work only if they are in the same directory or a subdirectory.

```js
sequencer.replaceImage(selector,steps);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you show what steps should be?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I shall add that.

@@ -0,0 +1,63 @@
<!DOCTYPE html>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this go in the examples folder, as just replace.html?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, sorry.

@jywarren
Copy link
Member

jywarren commented Jul 7, 2017

Also -- push a copy of this branch up to your gh-pages branch so we can try your demo! at https://ccpandhare.github.com/image-sequencer/examples/replace

@ccpandhare
Copy link
Collaborator Author

Yes. I will add more intensive tests for loadImage and include tests for replaceImage once we merge this in.

@ccpandhare
Copy link
Collaborator Author

ccpandhare commented Jul 8, 2017

The demo is now live at https://ccpandhare.github.io/image-sequencer/examples/replace !

Copy link
Member

@jywarren jywarren left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Merge?

Can options be set in steps on replaceImage? We don't have to, just wondering. Can we say in the docs that steps is just passed to addsteps, so usage is the same?

@ccpandhare
Copy link
Collaborator Author

Yes, options can be added. I can work on it. It is not exactly the same, as I haven't added support for options yet. It will hardly take a while. I will add that before merging. Is that okay?

Also this is my first demo for Image Sequencer! Do you like it? :-)

@jywarren
Copy link
Member

jywarren commented Jul 8, 2017 via email

@jywarren
Copy link
Member

jywarren commented Jul 8, 2017

Aha, very interesting! If you repeat clicking too long, the image degrades; we can address this in a follow-up issue but it's really cool to see, actually! Must be decimal/integer issues.

index 2

Yes go ahead and tweak the steps parameter as I think it's worth it for it to work the same way as addsteps, and you might even consider just passing it to addsteps to reduce code?

@ccpandhare
Copy link
Collaborator Author

Oh exciting! It works perfectly and looks great. Kudos!

Thanks!

Wow! Interesting!
Actually this has to do with save-pixels.
save-pixels doesn't save images with 100% quality by default. That's why the image quality degrades with every use.

and you might even consider just passing it to addsteps to reduce code?

That's exactly whats happening. This is the code behind replaceImages:

      this_.loadImage('default',url).addSteps('default',steps,options).run(function(out){
        the_image.src = out;
      });

@jywarren
Copy link
Member

jywarren commented Jul 8, 2017 via email

@ccpandhare
Copy link
Collaborator Author

We can simply pass {quality: 100} to addSteps and that should fix this.
I did that before but for some weird reason my build fails when I do so (One unit test fails -- Double Inversion). That was a very weird issue, to be honest. Two seemingly identical stings were not identical! Anyways, that's for another time.

@jywarren
Copy link
Member

jywarren commented Jul 8, 2017 via email

@ccpandhare
Copy link
Collaborator Author

Build Passed, merging it in!

@ccpandhare ccpandhare closed this Jul 8, 2017
@ccpandhare ccpandhare reopened this Jul 8, 2017
@ccpandhare ccpandhare merged commit 4a3b992 into publiclab:master Jul 8, 2017
@jywarren
Copy link
Member

jywarren commented Jul 8, 2017

🎈

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

Successfully merging this pull request may close these issues.

None yet

2 participants