首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >以角度表示的图像更改灰度(滑块)

以角度表示的图像更改灰度(滑块)
EN

Stack Overflow用户
提问于 2021-02-03 03:54:00
回答 2查看 122关注 0票数 0

我正在尝试使用滑块控件将图像转换为灰度和深褐色

这是我的html代码

代码语言:javascript
复制
       <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>

下面是我的答案

代码语言:javascript
复制
public set(e,f){
    document.getElementById('img_prev').style["filter"] = f+"("+e.value+")";
    document.getElementById('Amount_'+f).innerHTML="("+e.value+")";
 }

我收到错误消息

代码语言:javascript
复制
(index):13 Uncaught ReferenceError: set is not defined
at HTMLInputElement.oninput ((index):13)
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-02-03 04:20:16

为什么不使用“角度方式”呢?

您声明了两个变量

代码语言:javascript
复制
  sepia=0;
  grayScale=0;

只需使用[(ngModel)][style.filter]

代码语言:javascript
复制
<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">

请参阅simple stackblitz

票数 1
EN

Stack Overflow用户

发布于 2021-02-03 04:20:33

在上面的示例中,使用了oninput属性,它是一个global event attribute。这通常与普通的js一起使用,但在Angular中,还有一种在TypeScript代码中调用函数的方法:事件绑定。

在Angular中,不使用全局事件属性,而是使用Angular的事件绑定功能绑定到DOM Event

它遵循应使用(input)="set(..params..)"oninput="set(this, 'sepia');"更改为一种格式。

然而,一些适应似乎是必要的。例如,输入绑定中的this as参数将不起作用,$event很可能会包含必要的信息。另一方面,不推荐使用document.getElementById访问原生DOM元素,我建议使用ViewChild代替它。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66016717

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档