简体   繁体   English

JS:Function 在控制台中手动工作,但在代码中不自动执行

[英]JS: Function works manually in console but not executed automatically in code

I copied this code snippet from here: https://codepen.io/duketeam/pen/ALEByA我从这里复制了这个代码片段: https://codepen.io/duketeam/pen/ALEByA

This code is supposed to load a picture and make it grayscale after clicking on the button.此代码应该在单击按钮后加载图片并使其变为灰度。 I want to change the code such that the picture comes out immediately as grayscale without having to click on the button, hence why I commented it out and included makeGray() in the onload part of the <input...> .我想更改代码,使图片立即以灰度显示而无需单击按钮,因此我将其注释掉并将makeGray()包含在<input...>的 onload 部分中。 But this doesnt work.但这不起作用。 Where am I going wrong here?我在哪里错了?

I have tried to leave out the makeGray() and just upload the picture and type makeGray() in the console and it does its job perfectly fine, but I need it to execute automatically.我试图makeGray()并仅上传图片并在控制台中键入makeGray() ,它的工作非常好,但我需要它自动执行。 I also tried to just put all the code from the makeGray() function in the upload() function but also here it won't trigger.我还尝试将makeGray() function 中的所有代码放入upload() function 但这里也不会触发。

 var image = null; function upload() { //Get input from file input var fileinput = document.getElementById("finput"); //Make new SimpleImage from file input image = new SimpleImage(fileinput); //Get canvas var canvas = document.getElementById("can"); //Draw image on canvas image.drawTo(canvas); } function makeGray() { //change all pixels of image to gray for (var pixel of image.values()) { var avg = (pixel.getRed() + pixel.getGreen() + pixel.getBlue()) / 3; pixel.setRed(avg); pixel.setGreen(avg); pixel.setBlue(avg); } //display new image var canvas = document.getElementById("can"); image.drawTo(canvas); }
 <script src="https://www.dukelearntoprogram.com/course1/common/js/image/SimpleImage.js"> </script> <h1>Convert Image to Grayscale</h1> <canvas id="can"></canvas> <p> <input type="file" multiple="false" accept="image/*" id="finput" onchange="upload();makeGray()"> </p> <p> <.---- <input type="button" value="Make Grayscale" onclick="makeGray()" --> </p> <script src="./index.js"></script>

There's something you need to wait for, but I'm not sure exactly what it is.有些事情你需要等待,但我不确定它到底是什么。 If you wrap the function in a setTimeout , it works fine.如果将 function 包装在setTimeout中,则可以正常工作。 You might want to wait for a few more milliseconds for clients with slower systems or bigger files.对于系统较慢或文件较大的客户端,您可能需要再等待几毫秒。 If you ever figure out what is taking a while to do, you can instead use .then or await just to be safe.如果你知道什么需要一段时间来做,你可以改为使用.thenawait只是为了安全起见。

 var image = null; function upload() { //Get input from file input var fileinput = document.getElementById("finput"); //Make new SimpleImage from file input image = new SimpleImage(fileinput); //Get canvas var canvas = document.getElementById("can"); setTimeout(() => { //change all pixels of image to gray for (var pixel of image.values()) { var avg = (pixel.getRed() + pixel.getGreen() + pixel.getBlue()) / 3; pixel.setRed(avg); pixel.setGreen(avg); pixel.setBlue(avg); } image.drawTo(canvas); }, 100); }
 <script src="https://www.dukelearntoprogram.com/course1/common/js/image/SimpleImage.js"> </script> <h1>Convert Image to Grayscale</h1> <canvas id="can"></canvas> <p> <input type="file" multiple="false" accept="image/*" id="finput" onchange="upload();"> </p> <script src="./index.js"></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM