简体   繁体   English

angular中的图像更改灰度(滑块)

[英]Image change greyscale (Slider) in angular

I am trying to change image into Greyscale and Sepia using slider control我正在尝试使用 slider 控件将图像更改为灰度和棕褐色

Here is my html code这是我的 html 代码

       <div class="card-body">
        <input id="sepia" type="range" oninput="set(this, 'sepia');" value="0" step="0.1" min="0" max="1"> Sepia <span id="Amount_sepia">(0)</span><br/>
        <input id="grayscale" type="range" oninput="set(this, 'grayscale');" value="0" step="0.1" min="0" max="1"> Grayscale <span id="Amount_grayscale">(0)</span><br/>
      </div>

              <img class="img-fluid" id="img_prev" src="{{actualImage}}" *ngIf="!this.showCropper" />
                <image-cropper id="img_prev"  class="imageclass" *ngIf="this.showCropper" 
                        [autoCrop]="false"
                          [imageChangedEvent]="imageChangedEvent"
                           [maintainAspectRatio]="true"
                           [aspectRatio]="4 / 3"
                           [resizeToWidth]="256"
                           [cropperMinWidth]="128"
                            [onlyScaleDown]="true"
                           format="png"
                           (imageCropped)="imageCropped($event)"
                           (imageLoaded)="imageLoaded()"
                           (cropperReady)="cropperReady()"
                           (loadImageFailed)="loadImageFailed()" style="max-height:500px"> 
                </image-cropper>

Here is my ts for the same这是我的 ts

public set(e,f){
    document.getElementById('img_prev').style["filter"] = f+"("+e.value+")";
    document.getElementById('Amount_'+f).innerHTML="("+e.value+")";
 }

I am getting error我收到错误

(index):13 Uncaught ReferenceError: set is not defined
at HTMLInputElement.oninput ((index):13)

why not use an "Angular way"?为什么不使用“角度方式”?

You declare two variables你声明了两个变量

  sepia=0;
  grayScale=0;

And simply use [(ngModel)] and [style.filter]并且只需使用[(ngModel)][style.filter]

<input id="sepia" type="range" [(ngModel)]="sepia" 
    step="0.1" min="0" max="1"> Sepia
    <span id="Amount_sepia">({{sepia}})</span>
<br/>
<input id="grayscale" type="range" [(ngModel)]="grayScale" 
    step="0.1" min="0" max="1"> Grayscale
<span id="Amount_grayscale">({{grayScale}})</span>
<br/>
<img [style.filter]="'grayscale('+grayScale+') sepia('+sepia+')'" 
    src="https://picsum.photos/300/300?random=1">

See simple stackblitz查看简单的堆栈闪电战

In the above example oninput attribute is used which is a global event attribute .在上面的示例中,使用了oninput属性,它是一个全局事件属性 This is usually used with vanilla js but in Angular there is an other approach to call function in the TypeScript code: the event binding.这通常与 vanilla js 一起使用,但在 Angular 中有另一种方法可以在 TypeScript 代码中调用 function:事件绑定。

In Angular the global event attributes are not used, instead of them we bind to the DOM Event with the event binding feature of Angular.在 Angular 中,没有使用全局事件属性,而是使用 Angular 的事件绑定特性绑定到DOM 事件

It follows oninput="set(this, 'sepia');"它遵循oninput="set(this, 'sepia');" should be changed to a format with (input)="set(..params..)" .应更改为(input)="set(..params..)"的格式。

However, some adaptation seems to be necessary.然而,一些调整似乎是必要的。 For example the this as parameter in input binding won't work, most probably the $event will contain the necessary information.例如,输入绑定中的this作为参数不起作用,很可能$event将包含必要的信息。 On the other hand it is not recommended to access native DOM Elements using document.getElementById , I recommend to use ViewChild instead of it.另一方面,不建议使用document.getElementById访问本机 DOM 元素,我建议使用ViewChild代替它。

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

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