使用Js和H5绘制流程图

主要使用的项目

dagre-d3 | 详细文档

简化一下官方的一个demo为我所用
其中依赖的两个Js文件
https://d3js.org/d3.v4.min.js
https://dagrejs.github.io/project/dagre-d3/latest/dagre-d3.js

<!doctype html>

<meta charset="utf-8">
<title>Dagre D3 Demo: Sentence Tokenization</title>

<script src="/js/d3.v4.min.js" charset="utf-8"></script>
<script src="/js/dagre-d3.min.js"></script>

<style id="css">
    /* This sets the color for "TK" nodes to a light blue green. */
    g.type-TK > rect {
        fill: #00ffd0;
    }

    text {
        font-weight: 300;
        font-family: "Helvetica Neue", Helvetica, Arial, sans-serf;
        font-size: 14px;
    }

    .node rect {
        stroke: #999;
        fill: #fff;
        stroke-width: 1.5px;
    }

    .edgePath path {
        stroke: #333;
        stroke-width: 1.5px;
    }
</style>

<svg id="svg-canvas" width=960 height=600></svg>

<script >

  // Create the input graph
  var g = new dagreD3.graphlib.Graph()
    .setGraph({})
    .setDefaultEdgeLabel(function() { return {}; });

  // Here we"re setting nodeclass, which is used by our custom drawNodes function
  // below.
  g.setNode(0,  { label: "TOP",       class: "type-no" });
  g.setNode(1,  { label: "S",         class: "type-S" });
  g.setNode(2,  { label: "NP",        class: "type-NP" });
  g.setNode(3,  { label: "DT",        class: "type-DT" });
  g.setNode(4,  { label: "This",      class: "type-TK" });
  g.setNode(5,  { label: "VP",        class: "type-VP" });
  g.setNode(6,  { label: "VBZ",       class: "type-VBZ" });
  g.setNode(7,  { label: "is",        class: "type-TK" });
  g.setNode(8,  { label: "NP",        class: "type-NP" });
  g.setNode(9,  { label: "DT",        class: "type-DT" });
  g.setNode(10, { label: "an",        class: "type-TK" });
  g.setNode(11, { label: "NN",        class: "type-NN" });
  g.setNode(12, { label: "example",   class: "type-TK" });
  g.setNode(13, { label: ".",         class: "type-." });
  g.setNode(14, { label: "sentence",  class: "type-TK" });

  g.nodes().forEach(function(v) {
    var node = g.node(v);
    // Round the corners of the nodes
    node.rx = node.ry = 5;
  });

  // Set up edges, no special attributes.
  g.setEdge(3, 4);
  g.setEdge(2, 3);
  g.setEdge(1, 2);
  g.setEdge(6, 7);
  g.setEdge(5, 6);
  g.setEdge(9, 10);
  g.setEdge(8, 9);
  g.setEdge(11,12);
  g.setEdge(8, 11);
  g.setEdge(5, 8);
  g.setEdge(1, 5);
  g.setEdge(13,14);
  g.setEdge(1, 13);
  g.setEdge(0, 1);

  // Create the renderer
  var render = new dagreD3.render();

  // Set up an SVG group so that we can translate the final graph.
  var svg = d3.select("svg"),
    svgGroup = svg.append("g");

  // Run the renderer. This is what draws the final graph.
  render(d3.select("svg g"), g);

  // Center the graph
  var xCenterOffset = (svg.attr("width") - g.graph().width) / 2;
  svgGroup.attr("transform", "translate(" + xCenterOffset + ", 20)");
  svg.attr("height", g.graph().height + 40);
</script>

最终效果图

  • 该项目的demo很直接, 只需要添加Node, 然后添加每个节点的上下连接关系即可修改为自己的流程图
  • 个人改造 Github源码地址

这里写图片描述

在uniapp H5中获取声音频率并绘制声波图可以通过以下步骤实现: 1. 使用HTML5的Web Audio API获取麦克风输入的音频数据。 2. 将音频数据转换成频率数据,可以使用FFT算法进行频谱分析。 3. 将频率数据绘制成声波图,可以使用Canvas进行绘制。 以下是一个简单的示例代码: ```html <template> <canvas id="canvas"></canvas> </template> <script> export default { mounted() { this.draw(); }, methods: { draw() { const canvas = document.getElementById('canvas'); const ctx = canvas.getContext('2d'); // 获取麦克风输入的音频数据 navigator.mediaDevices.getUserMedia({ audio: true }) .then(stream => { const audioContext = new AudioContext(); const source = audioContext.createMediaStreamSource(stream); const analyser = audioContext.createAnalyser(); // 配置AnalyserNode,将音频数据转换成频率数据 analyser.fftSize = 2048; const bufferLength = analyser.frequencyBinCount; const dataArray = new Uint8Array(bufferLength); source.connect(analyser); // 每隔16.7毫秒获取一次频率数据,并绘制成声波图 setInterval(() => { analyser.getByteFrequencyData(dataArray); ctx.clearRect(0, 0, canvas.width, canvas.height); ctx.beginPath(); const barWidth = canvas.width / bufferLength; let x = 0; for (let i = 0; i < bufferLength; i++) { const barHeight = dataArray[i] / 2; ctx.fillStyle = `rgb(${barHeight}, 0, 0)`; ctx.fillRect(x, canvas.height - barHeight, barWidth, barHeight); x += barWidth + 1; } }, 16.7); }) .catch(err => { console.error(err); }); } } } </script> ``` 在上面的示例代码中,我们使用了HTML5的Web Audio API获取麦克风输入的音频数据,然后将音频数据转换成频率数据,并使用Canvas将频率数据绘制成声波图。你可以将上面的代码复制到你的uniapp项目中,并根据需要进行修改。
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值