面试题:React实现鼠标托转文字绕原点旋转

news/2025/2/3 13:33:07 标签: react.js, 前端, 前端框架

}

componentDidMount() {

document.onmousemove = (e) => {

if (this.state.moveFlag) {

let {pageX, pageY} = e;

// 1. 更改矩形位置

if (this.state.moveFlag) {

this.setState({

left: pageX - 25,

top: pageY - 10

})

}

// 2. 清空画布并绘制新的线

this._cleanCanvas((ctx) => {

this._drawNewLine(ctx, pageX, pageY);

});

}

}

}

render() {

const {left, top} = this.state

return (

ref={this.circle}

style={{left, top}}

className=‘text’>RPL10

<span

onMouseDown={(e) => this._mouseDown(e)}

onMouseUp={(e) => this._mouseUp(e)}

className=‘inside_circle’

/>

);

}

_mouseDown(e) {

this.setState({

moveFlag: true

})

this.circle.current.style.left = 50

}

_mouseUp(e) {

if (this.state.moveFlag) {

this.setState({

moveFlag: false

})

}

}

_cleanCanvas(drawNewLine) {

// 清空画布

let canvas = document.querySelector(‘#canvas’);

let ctx = canvas.getContext(‘2d’);

canvas.height = 800;

drawNewLine && drawNewLine(ctx)

}

_drawNewLine(ctx, left, top) {

ctx.moveTo(left, top);

// 4. 绘制直线

ctx.lineTo(401, 401);

// 5. 描边

ctx.stroke();

}

}

export default App;

App.css

*{

margin: 0;

padding: 0;

}

.app{

width: 800px;

height: 800px;

/background-color: red;/

position: relative;

border: 1px solid #000;

}

.circle{

height: 6px;

width: 6px;

border-radius: 50%;

background-color: orange;

position: absolute;

left: 50%;

top: 50%;

transform: translate(-3px);

}

.text{

padding: 0;

margin: 0;

border: 1px solid #999999;

font-weight: bolder;

position: absolute;

display: flex;

justify-content: center;

align-items: center;

z-index: 999;

user-select: none;

}

.inside_circle{

height: 6px;

width: 6px;

border-radius: 50%;

background-color: #00ff18;

position: absolute;

cursor: pointer;

}

版本2.0

================================================================

运行效果:

================================================================

在这里插入图片描述

思路:改进了一些地方,通过两直线相交,将起点改为了与DIV的交点。同时终点距离黄色圆圈有一定距离。

============================================================================================================

方法:

  1. 两直线相交

  2. 相似

React代码:

===================================================================

App.js


import ‘./App.css’;

import React from ‘react’

class App extends React.Component {

constructor(props) {

super(props);

this.circle = React.createRef();

this.state = {

left: 0,

top: 0,

moveFlag: false,

orangeX: 401,

orangeY: 404

}

}

componentDidMount() {

document.onmousemove = (e) => {

if (this.state.moveFlag) {

let {pageX, pageY} = e;

// 1. 更改矩形位置

if (this.state.moveFlag) {

this.setState({

left: pageX - 25,

top: pageY - 10

})

}

// 2. 清空画布并绘制新的线

this._cleanCanvas((ctx) => {

// 2.1 生成text和circle

const text = {

getBBox: () => {

return {

x: pageX,

y: pageY,

width: 50,

height: 20

}

}

}

const circle = {

center: {

x: 401,

y: 404

},

radius: 3

}

let res = this._around(text, circle)

this._drawNewLine(ctx, res);

// this._drawNewLine(ctx, pageX, pageY);

});

}

}

}

render() {

const {left, top} = this.state

return (

ref={this.circle}

style={{left, top}}

className=‘text’>RPL10

<span

onMouseDown={(e) => this._mouseDown(e)}

onMouseUp={(e) => this._mouseUp(e)}

className=‘inside_circle’

/>

);

}

_mouseDown(e) {

this.setState({

moveFlag: true

})

this.circle.current.style.left = 50

}

_mouseUp(e) {

if (this.state.moveFlag) {

this.setState({

moveFlag: false

})

}

}

_cleanCanvas(drawNewLine) {

// 清空画布

let canvas = document.querySelector(‘#canvas’);

let ctx = canvas.getContext(‘2d’);

canvas.height = 800;

drawNewLine && drawNewLine(ctx)

}

_drawNewLine(ctx, linePoint) {

ctx.moveTo(linePoint.startPt.x, linePoint.startPt.y);

// 4. 绘制直线

ctx.lineTo(linePoint.endPt.x, linePoint.endPt.y);

// 5. 描边

ctx.stroke();

}

_around(text, circle) {

const bBox = text.getBBox();

const centerBox = {

x: bBox.x,

y: bBox.y,

};

const centerCircle = {

x: circle.center.x,

y: circle.center.y

}

const radius = circle.radius;

const endPtDistCircle = 3;

let lineSeg = {

startPt: {

x: undefined,

y: undefined

},

endPt: {

x: undefined,

y: undefined

}

}

// 计算lineSeq

// 1. 获取正方形四个点,根据不同情况,判断四点交点,计算起始点

const leftTop = {

x: centerBox.x - bBox.width * 0.5,

y: centerBox.y - bBox.height * 0.5

};

const leftBottom = {

x: centerBox.x - bBox.width * 0.5,

y: centerBox.y + bBox.height * 0.5


http://www.niftyadmin.cn/n/5840847.html

相关文章

Ubuntu 下 nginx-1.24.0 源码分析 - ngx_strerror_init()函数

目录 ngx_strerror_init()函数声明 ngx_int_t 类型声明定义 intptr_t 类型 ngx_strerror_init()函数实现 NGX_HAVE_STRERRORDESC_NP ngx_strerror_init()函数声明 在 nginx.c 的开头引入了: #include <ngx_core.h> 在 ngx_core.h 中引入了 #include <ngx_er…

【回溯+剪枝】回溯算法的概念 全排列问题

文章目录 46. 全排列Ⅰ. 什么是回溯算法❓❓❓Ⅱ. 回溯算法的应用1、组合问题2、排列问题3、子集问题 Ⅲ. 解题思路&#xff1a;回溯 剪枝 46. 全排列 46. 全排列 ​ 给定一个不含重复数字的数组 nums &#xff0c;返回其 所有可能的全排列 。你可以 按任意顺序 返回答案。 …

HTB:Alert[WriteUP]

目录 连接至HTB服务器并启动靶机 信息收集 使用rustscan对靶机TCP端口进行开放扫描 使用nmap对靶机TCP开放端口进行脚本、服务扫描 使用nmap对靶机TCP开放端口进行漏洞、系统扫描 使用nmap对靶机常用UDP端口进行开放扫描 使用ffuf对alert.htb域名进行子域名FUZZ 使用go…

pytorch基于GloVe实现的词嵌入

PyTorch 实现 GloVe&#xff08;Global Vectors for Word Representation&#xff09; 的完整代码&#xff0c;使用 中文语料 进行训练&#xff0c;包括 共现矩阵构建、模型定义、训练和测试。 1. GloVe 介绍 基于词的共现信息&#xff08;不像 Word2Vec 使用滑动窗口预测&…

C++ 哈希封装详解

文章目录 1.buckets1.1 load_factor和max_load_factor1.2 reserve和rehash1.3 bucket_count1.4 bucket_size 2.哈希表的底层2.1 iterator的实现2.2 operator2.3 HashTable.h2.4 Unorderedmap.h2.5 Uorderedset.h 1.buckets 1.1 load_factor和max_load_factor load_factor负载…

JavaScript系列(54)--性能优化技术详解

JavaScript性能优化技术详解 ⚡ 今天&#xff0c;让我们继续深入研究JavaScript的性能优化技术。掌握这些技术对于构建高性能的JavaScript应用至关重要。 性能优化基础概念 &#x1f3af; &#x1f4a1; 小知识&#xff1a;JavaScript性能优化涉及多个方面&#xff0c;包括代…

js对象方法大全

JavaScript中Object构造函数的方法 Object构造函数的方法节 Object.assign() 通过复制一个或多个对象来创建一个新的对象。 Object.create() 使用指定的原型对象和属性创建一个新对象。 Object.defineProperty() 给对象添加一个属性并指定该属性的配置。 Object.definePr…

【JavaScript】Web API事件流、事件委托

目录 1.事件流 1.1 事件流和两个阶段说明 1.2 事件捕获 1.3 事件冒泡 1.4 阻止冒泡 1.5 解绑事件 L0 事件解绑 L2 事件解绑 鼠标经过事件的区别 两种注册事件的区别 2.事件委托 案例 tab栏切换改造 3.其他事件 3.1 页面加载事件 3.2 页面滚动事件 3.2 页面滚…