<div id="box" #box>
<img class="image" src = "https://img0.baidu.com/it/u=2862534777,914942650&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500"/>
</div>
<textarea class="input" disabled type="text" name="content" [value]="values"></textarea>
#box{
width: 500px;
height: 500px;
border: 1px solid black;
margin: 20px auto;
position: relative;
}
.image{
width: 500px;
height: 500px;
}
.input{
position: absolute;
right:30%;
top:10%;
background:rgba(255,255,255,0.15);
z-index:999;
width:326px;
height:65px;
padding:12px
}
import { Component, ViewChild, ElementRef, AfterViewInit, Renderer2} from '@angular/core';
@Component({
selector: 'app-blackboard',
templateUrl: './blackboard.component.html',
styleUrls: ['./blackboard.component.scss']
})
export class BlackboardComponent implements AfterViewInit {
@ViewChild('box')
oBox!: ElementRef<HTMLInputElement>;
offsetLeft = 0;
offsetTop = 0;
values = "";
constructor(private renderer: Renderer2) {}
ngOnInit() {}
ngAfterViewInit() {
let thiss = this;
thiss.offsetLeft = thiss.oBox.nativeElement.offsetLeft;
thiss.offsetTop = thiss.oBox.nativeElement.offsetTop;
thiss.oBox.nativeElement.onmousedown = function (ev: { clientX: number; clientY: number; }) {
var x1 = ev.clientX - thiss.offsetLeft;
var y1 = ev.clientY - thiss.offsetTop;
var oDiv = document.createElement("div");
thiss.renderer.appendChild(thiss.oBox.nativeElement,oDiv)
thiss.oBox.nativeElement.onmousemove = function (ev: { clientX: number; clientY: number; }) {
var x2 = ev.clientX - thiss.offsetLeft;
var y2 = ev.clientY - thiss.offsetTop;
oDiv.style.left = (x2 > x1 ? x1 : x2) +"px";
oDiv.style.top = (y2 > y1 ? y1 : y2) +"px";
oDiv.style.width = Math.abs(x2-x1)+"px";
oDiv.style.height =Math.abs(y2-y1)+"px";
oDiv.style.border = "2px solid red";
oDiv.style.position = "absolute";
}
return false;
}
this.oBox.nativeElement.onmouseup = function () {
alert(111)
thiss.oBox.nativeElement.onmousemove = null;
}
}
}