html 正弦振荡器
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了html 正弦振荡器相关的知识,希望对你有一定的参考价值。
A little function that makes a sine wave generator with some simple properties:
the range over which it varies and the wavelength
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Test</title>
<script src="https://d3js.org/d3.v5.min.js"></script>
<script src="index.js"></script>
</head>
<body>
<svg width="500" height="500"></svg>
</body>
<script>
//default is that it oscilates between -1 and 1
//with one complete oscilation hapening every 1 unit of time
function makeSinOscilator(range=[-1,1], waveLength = 1){
waveLength = waveLength/(2*Math.PI);
const scaleX = d3.scaleLinear()
.domain([-1,1])
.range(range)
return (time) => scaleX( Math.sin(time/waveLength) );
}
const myOsc = makeSinOscilator([-2,2],0.5);
const values = [];
for(let t = 0; t<1; t += 0.01){
values.push({x:t, y:myOsc(t)});
}
const scaleX = d3.scaleLinear()
.range([0,500])
.domain(d3.extent(values, (d)=>d.x));
const scaleY = d3.scaleLinear()
.range([480,20])
.domain([-5, 5]);
const line = d3.line()
.x(d => scaleX(d.x))
.y(d => scaleY(d.y));
d3.select('svg')
.selectAll('path')
.data([values])
.enter()
.append('path')
d3.select('svg')
.selectAll('line')
.data([-5,-4,-3,-2,-1,0,1,2,3,4,5])
.enter()
.append('line')
.attr('fill','none')
.attr('stroke',d=>{
if(d==0) return 'red';
return 'blue'
})
.attr('strke-width',2)
.attr('x1', scaleX(0))
.attr('x2', scaleX(scaleX.domain()[1]))
.attr('y1', d => scaleY(d))
.attr('y2', d => scaleY(d));
function update(){
d3.select('path')
.attr('fill','none')
.attr('stroke','black')
.attr('d', line);
}
update();
</script>
</html>
以上是关于html 正弦振荡器的主要内容,如果未能解决你的问题,请参考以下文章
用于镶边效果的 Java 正弦振荡器
基本正弦波振荡器上的无毛刺频率变化
LM7805的另类的使用方法:构成一个正弦振荡器
高频电子线路实验手册(全套实验报告册)
javascript 在正弦路径之后的两点之间的振荡
全套完结高频电子线路实验手册——全套实验报告册