由于时效问题,该文某些代码、技术可能已经过期,请注意!!!本文最后更新于:1 年前
3dmol React Demo
创建项目
导入3dmol.js
因为3dmol.js只是一个普通的js库,所以这里在index.html中引入,代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <link rel="icon" href="%PUBLIC_URL%/favicon.ico" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <meta name="theme-color" content="#000000" /> <meta name="description" content="Web site created using create-react-app" /> <link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" /> <script src="https://cdn.bootcdn.net/ajax/libs/3Dmol/2.0.1/3Dmol-min.min.js"></script>
<title>React App</title> </head> <body> <noscript>You need to enable JavaScript to run this app.</noscript> <div id="root"></div> </body> </html>
|
编写APP.js 和 APP.css
APP.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| import React, { useRef, useEffect }from 'react'; import './App.css';
function App() { const containerRef = useRef(null); const $3Dmol = window.$3Dmol;
useEffect(() => { const element = containerRef.current; if (element) { let viewer = $3Dmol.createViewer(element);
$3Dmol.download("pdb:1MO8", viewer, { multimodel: true, frames: true }, function () { viewer.setStyle({}, { cartoon: { color: 'spectrum' } }); viewer.render(); }); } }, []);
return ( <div className="App"> <div className="display-box" id="protein-box" ref={containerRef}></div> </div> ); }
export default App;
|
APP.css , 主要是 display-box 的样式设置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| .App { text-align: center; }
.App-logo { height: 40vmin; pointer-events: none; } .display-box{ height: 600px; width: 800px; position: relative; }
@media (prefers-reduced-motion: no-preference) { .App-logo { animation: App-logo-spin infinite 20s linear; } }
.App-header { background-color: #282c34; min-height: 100vh; display: flex; flex-direction: column; align-items: center; justify-content: center; font-size: calc(10px + 2vmin); color: white; }
.App-link { color: #61dafb; }
@keyframes App-logo-spin { from { transform: rotate(0deg); } to { transform: rotate(360deg); } }
|
运行
执行命令
就可以在浏览器看到蛋白了。
完整代码仓库:
https://github.com/shubihu/3dmol-react-example
参考:https://juejin.cn/post/7118732948328677389